Java 如何在Iterable中的event_date字段中获取具有最高事件日期的对象的唯一列表<;学生>;收集
我有一个Java 如何在Iterable中的event_date字段中获取具有最高事件日期的对象的唯一列表<;学生>;收集,java,arraylist,collections,java-8,java-stream,Java,Arraylist,Collections,Java 8,Java Stream,我有一个Iterable studentList,其中包含带有id、event\u date等字段的student对象 Iterable<Student> myStudentList (contains student objects); 预期结果 现在我的resultIterable列表应该包含以下内容 ListIndex | Student ID | Student Name | Student Event Date | 0 RV001
Iterable studentList
,其中包含带有id、event\u date等字段的student对象
Iterable<Student> myStudentList (contains student objects);
预期结果
现在我的resultIterable
列表应该包含以下内容
ListIndex | Student ID | Student Name | Student Event Date |
0 RV001 Mahesh 1549457671000
1 RV002 James 1549457671000
2 RV003 Andy 1454763174000
如何使用Java8高效地实现这一点
此数据来自按事件日期划分的表
我目前能想到的方法是:-p
Step 1 - Create a HashMap<Student_id, List<Student>>
Step 2 - Create a final List<Student> finalList for result purpose.
Step 2 - Iterate through the List<Student>
(a) For each student_id , store the list of Student objects in the
Map
-- END of Iterating List<Student>
Step 3 - Iterate Each Map Entry in HashMap
(a) For every key , Apply Comparator on List<Student> for
current student id.
(b) Get the first object of List<Student> in map for current
student_id and add it to the final list
步骤1-创建哈希映射
第2步-创建一个最终列表最终列表,用于结果。
步骤2-遍历列表
(a) 对于每个学生id,将学生对象列表存储在
地图
--迭代列表结束
步骤3-迭代HashMap中的每个映射项
(a) 对于每个键,应用列表上的比较器
当前学号。
(b) 获取当前映射中列表的第一个对象
学生id并将其添加到最终列表中
如果您有更好的方法,请告诉我。您可能正在寻找以下方法:
List<Student> finalList = students.stream()
// group by Id, value as List
.collect(Collectors.groupingBy(Student::getId,
// reduce the list of values to only one with greater event_date
Collectors.reducing((student, student2) ->
student.getEvent_date() > student2.getEvent_date() ? student : student2)))
.values() // stream only such values
.stream()
.map(Optional::get)
.collect(Collectors.toList()); // collect to list
这将需要您将此处形成的映射的值包装在
新ArrayList()
中,类似于@nullpointer answer,但假设事件日期具有int
、long
或double
类型,您可以使用maxBy
下游与comparingit
,comparingLong
或comparingDouble
比较器
studentList.stream()
.collect(groupingBy(Student::getId,
maxBy(Comparator.comparingLong(Student::getEventDate))))
.values()
.stream()
.map(Optional::get)
.collect(toList());
查找最高事件日期,然后按日期值筛选学生代码和数据结构在哪里?这看起来像是试图将来自数据库的结果集转换为对象结构,但操作错误。列表中不应该有几个“James”,而是有一个“James”有多个事件,最后还有一个方法“findMostRecentEventDate()”@spi-是的,你是对的,但这是分区表,我收到了问题中规定的数据。什么类型的对象事件日期?我考虑了
长类型的事件日期。每当下游收集器封装一个缩减,迫使您处理可选结果时,考虑使用<代码> GoopPix:<代码>收藏夹(收藏家.ToMAP(学生::GETID,函数.IdIsId),(S1,S2)-S.GETEngEndoDATE())SG.EngEndoDATA(,S1:S2)< <代码> >,霍格尔注意到,在<代码>某个点上感到疑惑.GET < /代码>没有<代码> IsSt/ <代码>检查。仅使用Java 8的内置收集器,您可以假设减少groupingBy
的组永远不会导致空的可选的,因此,在这个孤立的场景中,您可以跳过isPresent
检查。但是,对于自定义收集器或Java 9的过滤
或平面映射
收集器,这种假设可能不再成立。因此,在不同的情况下重用这样的代码是很脆弱的。实际上,maxBy
内部只会reduce
相当于reduce(BinaryOperator.maxBy(comparator))
.collect(Collectors.toMap(Student::getId, Function.identity(),
(s1, s2) -> s1.getEvent_date() > s2.getEvent_date() ? s1: s2))
studentList.stream()
.collect(groupingBy(Student::getId,
maxBy(Comparator.comparingLong(Student::getEventDate))))
.values()
.stream()
.map(Optional::get)
.collect(toList());