亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

集合中的 event_date 字段中獲取具有最高事件日期的對象的唯一列表

集合中的 event_date 字段中獲取具有最高事件日期的對象的唯一列表

Helenr 2022-06-08 16:55:42
我有一個Iterable<Student> studentList包含字段 id、event_date 等等的學生對象。Iterable<Student> myStudentList (contains student objects);現在這個學生列表包含一個學生 id 的許多對象。假設我需要修改此學生列表,使其僅包含少數學生對象(每個學生 id 一個,并且該學生對象應包含其 student_id 的所有學生對象的最大 event_date )。我如何有效地做到這一點?例如。目前該列表包含以下項目ListIndex  |  Student ID | Student Name | Student Event Date |0            RV001         Mahesh          13285327740001            RV002         James           13285327740002            RV002         James           14547631740003            RV002         James           15494576710004            RV001         Mahesh          15494576710005            RV003         Andy            1454763174000預期結果現在我的結果Iterable<Student>列表應該包含以下結果ListIndex  |  Student ID | Student Name | Student Event Date |0            RV001         Mahesh          15494576710001            RV002         James           15494576710002            RV003         Andy            1454763174000我如何使用 java 8 有效地實現這一目標?此數據來自按 event_date 分區的表我目前能想到的方法:-PStep 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如果您有更好的方法,請告訴我。
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

您可能只是在尋找類似的東西:


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

或者正如霍爾格指出的那樣,您可以使用


.collect(Collectors.toMap(Student::getId, Function.identity(), 

                  (s1, s2) -> s1.getEvent_date() > s2.getEvent_date() ? s1: s2))

這需要您將此處形成的映射值包裝在new ArrayList<>().


查看完整回答
反對 回復 2022-06-08
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

與@nullpointer 答案類似,但假設事件日期有或類型,您可以使用int下游with或比較器longdoublemaxBycomparingIntcomparingLongcomparingDouble

studentList.stream()
    .collect(groupingBy(Student::getId, 
             maxBy(Comparator.comparingLong(Student::getEventDate))))
    .values()
    .stream()
    .map(Optional::get)
    .collect(toList());


查看完整回答
反對 回復 2022-06-08
  • 2 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號