You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从分组后的Timestamp-List<STUP>映射中获取最新月份数据

Solution to Extract Latest Month Data from Grouped Map

Let's walk through how to pull out all entries from your grouped map that fall into the most recent month present in the data.

Step 1: Identify the Latest Month First

First, we need to figure out which year-month is the newest in your map's keys. We can convert each Timestamp to a YearMonth object (which captures both month and year), then find the maximum (latest) one using streams:

import java.time.YearMonth;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

// Find the latest year-month in the map's keys
Optional<YearMonth> optionalLatestMonth = timestampListMap.keySet().stream()
    .filter(timestamp -> timestamp != null) // Skip any null dates to avoid errors
    .map(timestamp -> timestamp.toLocalDateTime().toYearMonth())
    .max(YearMonth::compareTo);

// Handle empty map or all null dates case
if (optionalLatestMonth.isEmpty()) {
    throw new IllegalStateException("No valid transaction dates found in the map");
}

YearMonth latestMonth = optionalLatestMonth.get();

Step 2: Filter Data for the Latest Month

Now we can filter the map to keep only entries where the transaction date falls within that latest month:

import java.util.stream.Collectors;

// Get a map of all entries from the latest month
Map<Timestamp, List<STUP>> latestMonthData = timestampListMap.entrySet().stream()
    .filter(entry -> {
        Timestamp timestamp = entry.getKey();
        return timestamp != null && timestamp.toLocalDateTime().toYearMonth().equals(latestMonth);
    })
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

// Or, if you want a single combined list of all STUPs from the latest month
List<STUP> allLatestMonthStups = timestampListMap.entrySet().stream()
    .filter(entry -> {
        Timestamp timestamp = entry.getKey();
        return timestamp != null && timestamp.toLocalDateTime().toYearMonth().equals(latestMonth);
    })
    .flatMap(entry -> entry.getValue().stream())
    .collect(Collectors.toList());

Quick Notes:

  • Null Safety: The filter(timestamp -> timestamp != null) checks prevent crashes if some STUP objects have missing transaction dates.
  • Truncated Dates: Your initial code truncates dates to days but doesn't use that result. If you meant to group by truncated dates instead of the original Timestamp, adjust the grouping step like this:
    Map<LocalDate, List<STUP>> dateListMap = rupFeedDC.stream()
        .filter(rl -> rl.getTransactionDate() != null)
        .collect(Collectors.groupingBy(rl -> rl.getTransactionDate().toLocalDateTime().truncatedTo(ChronoUnit.DAYS).toLocalDate()));
    
    Then use localDate.getYearMonth() instead of converting from Timestamp in the rest of the logic.

内容的提问来源于stack exchange,提问作者kaviya .P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:10:15