holdtom
C#代码已经由@Rango解释过了。假设 C 中的 -类采用以下设计#Logclass Log{ public String title; public String ID; public DateTime dataChangeTime; public String whatever; ...}和类似的Java类(例如 而不是 ),提供相同结果的 Java 表达式是:LocalDateTimeDateTimeComparator<Log> comparator = (Log l1, Log l2) -> l2.dataChangeTime.compareTo(l1.dataChangeTime); // sort descendingList<Log> resultantList = initialList.stream() .collect(Collectors.groupingBy(l -> l.title + l.ID)).values().stream() // group according to title and id .map(logs -> logs.stream().sorted(comparator).findFirst().get()) // sort and take the first .collect(Collectors.toList()); // create the list表达式将所有具有相同标题和 ID 的对象组合在一起,即 相同的值 。如果分组条件更复杂,那么定义一个表示分组的类可能更有意义,例如Logl.title + l.IDclass LogGroup { private String Title; private String ID; public LogGroup(String Title, String ID) { this.Title = Title; this.ID = ID; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof LogGroup)) { return false; } LogGroup logGroup = (LogGroup) o; return Objects.equals(Title, logGroup.Title) && Objects.equals(ID, logGroup.ID); } @Override public int hashCode() { return Objects.hash(Title, ID); }}至关重要的是,类同时实现 - 和 -方法(即仅实现 -方法是不够的)。equalshashCodeequals使用该类,Java 表达式将变为:Comparator<Log> comparator = (Log l1, Log l2) -> l2.dataChangeTime.compareTo(l1.dataChangeTime);List<Log> resultantList = initialList.stream() .collect(Collectors.groupingBy(l -> new LogGroup(l.title, l.ID))).values().stream() .map(logs -> logs.stream().sorted(comparator).findFirst().get()) .collect(Collectors.toList());像这样的列表private static List<Log> getInitialList() { List<Log> initialList = new ArrayList<>(); initialList.add(new Log("Title 6", "ID 6", LocalDateTime.of(2017, 1, 18, 23, 15, 12), "A")); initialList.add(new Log("Title 3", "ID 3", LocalDateTime.of(2005, 4, 20, 16, 10, 10), "B")); initialList.add(new Log("Title 1", "ID 1", LocalDateTime.of(2010, 10, 25, 3, 5, 2), "C")); initialList.add(new Log("Title 2", "ID 2", LocalDateTime.of(2018, 2, 18, 21, 13, 32), "D")); initialList.add(new Log("Title 3", "ID 3", LocalDateTime.of(2016, 5, 16, 15, 23, 15), "E")); initialList.add(new Log("Title 1", "ID 1", LocalDateTime.of(2012, 2, 8, 14, 46, 28), "F")); initialList.add(new Log("Title 6", "ID 6", LocalDateTime.of(1996, 1, 28, 22, 26, 34), "G")); initialList.add(new Log("Title 3", "ID 3", LocalDateTime.of(2007, 4, 15, 2, 5, 55), "H")); initialList.add(new Log("Title 6", "ID 3", LocalDateTime.of(2018, 1, 15, 20, 15, 10), "I")); return initialList;}由两个表达式处理,如下所示Title 1 ID 1 2012-02-08 14:46:28 FTitle 3 ID 3 2016-05-16 15:23:15 ETitle 2 ID 2 2018-02-18 21:13:32 DTitle 6 ID 6 2017-01-18 23:15:12 ATitle 6 ID 3 2018-01-15 20:15:10 I生成的列表本身没有排序(这很容易实现),但这也适用于 C#输出。Collections.sort(...)