繁体   English   中英

Java 8流减少了保留最新条目的删除重复项

[英]Java 8 Streams reduce remove duplicates keeping the most recent entry

我有一个Java bean,例如

class EmployeeContract {
    Long id;
    Date date;
    getter/setter
}

如果其中有很长的列表,其中我们有ID重复但日期不同的重复项,例如:

1, 2015/07/07
1, 2018/07/08
2, 2015/07/08
2, 2018/07/09

如何减少这样的列表,使其仅保留具有最新日期的条目,例如:

1, 2018/07/08
2, 2018/07/09

最好使用Java 8 ...

我从类似以下内容开始:

contract.stream()
         .collect(Collectors.groupingBy(EmployeeContract::getId, Collectors.mapping(EmployeeContract::getId, Collectors.toList())))
                    .entrySet().stream().findFirst();

这使我可以在各个组中进行映射,但是我对如何将其收集到结果列表中感到困惑-恐怕我的流不太强...

好吧,我将在这里以回答的形式发表我的评论:

 yourList.stream()
         .collect(Collectors.toMap(
                  EmployeeContract::getId,
                  Function.identity(),
                  BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
            )
         .values();

如果您真的很在意这会给您一个Collection而不是一个List

您可以按照以下两个步骤进行操作:

List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
        .collect(Collectors.toMap(EmployeeContract::getId, 
                EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
        .entrySet().stream() // Stream<Entry<Long, Date>>
        .map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
        .collect(Collectors.toList()); // Step 2

第一步 :确保将date s与映射到iddate进行比较。

第二步 :将这些键值对映射到最终的List<EmployeeContract>

使用vavr.io,您可以这样操作:

var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
            .groupBy(EmployeeContract::getId)
            .map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
            .collect(Collectors.toList()); //result is list from java.util package

您只想补充现有的答案,就可以:

如何将其收集到结果列表中

以下是一些选项:

  • values()包装到ArrayList

     List<EmployeeContract> list1 = new ArrayList<>(list.stream() .collect(toMap(EmployeeContract::getId, identity(), maxBy(comparing(EmployeeContract::getDate)))) .values()); 
  • toMap收集器包装到collectingAndThen

     List<EmployeeContract> list2 = list.stream() .collect(collectingAndThen(toMap(EmployeeContract::getId, identity(), maxBy(comparing(EmployeeContract::getDate))), c -> new ArrayList<>(c.values()))); 
  • 使用另一个流将values收集到新的List中:

     List<EmployeeContract> list3 = list.stream() .collect(toMap(EmployeeContract::getId, identity(), maxBy(comparing(EmployeeContract::getDate)))) .values() .stream() .collect(toList()); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM