我有一个list分,看起来像下面这样

[
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-27 05:22:46",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 1,
    "currentSpeed": 67,
    "passengersNo": 80
  },
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-27 07:12:10",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 2,
    "currentSpeed": 56,
    "passengersNo": 11
  },
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-26 03:12:10",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 3,
    "currentSpeed": 31,
    "passengersNo": 15
  },
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-26 03:12:10",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 3,
    "currentSpeed": 78,
    "passengersNo": 15
  },
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-26 04:34:10",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 7,
    "currentSpeed": 49,
    "passengersNo": 57
  }
]

我想要实现的是过滤,例如,分别在日期2022-08-27和日期2022-08-26,我只返回一个最大值为currentSpeed的记录,这样我的最终列表如下所示

[
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-27 05:22:46",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 1,
    "currentSpeed": 67,
    "passengersNo": 80
  },
  {
    "busId": "4-3323309834",
    "dataDateTime": "2022-08-26 03:12:10",
    "busName": "27Tr Solaris Single Deck",
    "speedLimit": 80,
    "id": 3,
    "currentSpeed": 78,
    "passengersNo": 15
  }
]

下面是我的代码外观以及我填充列表的方式

public static void main(String[] args) {

        List<HashMap<String, Object>> myList = new ArrayList<>();

        myList.add(new HashMap<>(Map.of("id", 1,
                "busId","4-3323309834",
                "busName","27Tr Solaris Single Deck",
                "currentSpeed",67,
                "passengersNo",80,
                "speedLimit",80,
                "dataDateTime","2022-08-27 05:22:46")));

        myList.add(new HashMap<>(Map.of("id",2,
                "busId","4-3323309834",
                "busName","27Tr Solaris Single Deck",
                "currentSpeed",56,
                "passengersNo",11,
                "speedLimit",80,
                "dataDateTime","2022-08-27 07:12:10")));

        myList.add(new HashMap<>(Map.of(
                "id",3,
                "busId","4-3323309834",
                "busName","27Tr Solaris Single Deck",
                "currentSpeed",31,
                "passengersNo",15,
                "speedLimit",80,
                "dataDateTime","2022-08-26 03:12:10")));

        myList.add(new HashMap<>(Map.of(
                "id",3,
                "busId","4-3323309834",
                "busName","27Tr Solaris Single Deck",
                "currentSpeed",78,
                "passengersNo",15,
                "speedLimit",80,
                "dataDateTime","2022-08-26 03:12:10")));

        myList.add(new HashMap<>(Map.of(
                "id",7,
                "busId","4-3323309834",
                "busName","27Tr Solaris Single Deck",
                "currentSpeed",49,
                "passengersNo",57,
                "speedLimit",80,
                "dataDateTime","2022-08-26 04:34:10")));

    }

下面是我试图使用的过滤器,但我在下面的过滤器代码中得到了Not a statement

        List<HashMap<String, Object>> myList2 = myList.stream()
                .collect(Collectors.groupingBy(hashmap ->
                                List.of(hashmap.get("busId"),
                                        hashmap.get("currentSpeed"),
                        Collectors.maxBy(Comparator.comparing(HashMap::get("dataDateTime")))));




        System.out.println(Config.ANSI_CYAN + "MyListToJsonPrint: " +
                new GsonBuilder().setPrettyPrinting().create().toJson(myList2));

有没有一种有效的方法可以用来过滤

推荐答案

如果需要每个day的最大值为currentSpeed的映射,则需要按day分组并映射到当前速度为最大值的散列图.您可以使用java.timeAPI中的LocalDateTime来解析您的日期,以便更容易地将您的映射值用作分组的分类器.

基本上,您需要的步骤是:

  • 在你的 list 上串连
  • 使用日期作为关键字采集到 map
  • 使用BinaryOperatorComparator映射到最大值为currentSpeed的哈希图
  • 上述步骤将产生一个Map<LocalDate, HashMap<String, Object>>
  • 并最终从上面的 map 中获得值

代码:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

List<HashMap<String, Object>> result = new ArrayList<>(
        myList.stream()
              .collect(Collectors.toMap(map -> LocalDateTime.parse((String) map.get("dataDateTime"), dtf).toLocalDate(),
                                        Function.identity(),
                                        BinaryOperator.maxBy(Comparator.comparingInt(map -> (int)map.get("currentSpeed")))))
              .values());

result.forEach(System.out::println);

您也可以将Collectors.groupingByCollectors.reducing组合使用,但随后需要展开从减法步骤得到的可选结果

List<HashMap<String, Object>> result2 = new ArrayList<>(
myList.stream()
       .collect(Collectors.groupingBy(map -> LocalDateTime.parse((String) map.get("dataDateTime"), dtf).toLocalDate(),
                                      Collectors.collectingAndThen(
                                              Collectors.reducing(BinaryOperator.maxBy(Comparator.comparingInt(map -> (int)map.get("currentSpeed")))),
                                              Optional::get))).values());

result2.forEach(System.out::println);

Java相关问答推荐

如何跟踪我在数组中的位置

将具有多个未知字段的SON映射到Java POJO

如何审查Java dtos中的自定义注释字段?

在Java 8之后,HashMap的最坏情况下时间复杂度仍然是O(n)而不是O(log n)?

Springdoc Whitelabel Error Page with Spring V3

方法没有用正确的值填充数组—而是将数组保留为null,'

为什么一个java函数会返回一个作为参数传递给它的对象?

关于泛型的覆盖规则

按属性值从流中筛选出重复项

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

有效的公式或值列表必须少于或等于255个字符

使用正则表达式从字符串中提取多个值

如何获得凌空cookies ,并设置它在下一个请求- android

Quarkus:运行时出现EnumConstantNotPresentException

IntelliJ IDEA依赖项工具窗口丢失

为什么相同的数据条码在视觉上看起来不同?

不能在 map 上移除折线

在单例类上获取Java锁,了解原因

Java中计算大n和k值模10^9+7的二项式系数的乘法公式输出错误值

可以';不要在Intellij IDEA中使用最新的Java版本(JDK 21)