Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Iterable中的event_date字段中获取具有最高事件日期的对象的唯一列表<;学生>;收集_Java_Arraylist_Collections_Java 8_Java Stream - Fatal编程技术网

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);
预期结果

现在我的result
Iterable
列表应该包含以下内容

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());