猿问

.net list group by in java 8?

我得到了一个将代码从C#迁移到java 8的任务。我对下面的C#代码有问题。


List<Log> lst = LogRepository.GetLogs(DateTime.Now.AddDays(-2), DateTime.Now);


return lst

.GroupBy(x => new { x.Title, x.ID })

.Select(x => x.OrderByDescending(y => y.DataChangeTime).FirstOrDefault())

.ToList();

是的,方法GroupBy很容易,我知道它在做什么。但是,我无法弄清楚这个系列方法在做什么,它会返回什么结果?最后,任何人都可以给我一个java版本解决方案吗?


芜湖不芜
浏览 127回答 1
1回答

holdtom

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