對兩個List進行關聯匹配,選擇匹配上的記錄形成新的List輸出


Java 8 Stream Convert  Stream<List<String>> to List<String>

【目標】:對兩個集合中的元素通過特定指標進行匹配,重新組合匹配成功的元素。(如下圖:)

【說明】:對兩個集合 listA、listB 依據 field1 字段進行匹配,若匹配成功,則取出 listA 中的 field0、field1 和 listB 中的 field2 組合成新元素;多個匹配項形成新的 list:commonList.

【關鍵】:java8 Stream處理: list.stream().*其他操作*().flatMap() 實現合並。

   【關鍵代碼示例】:

【完整代碼示例】:

 1     @Data
 2     class Person {
 3         private String userName;
 4         private int bloodVolume;
 5         public Person(String userName, int bloodVolume) {
 6             this.userName = userName;
 7             this.bloodVolume = bloodVolume;
 8         }
 9     }
10 
11     @Data
12     class Rule {
13         private String userName;
14         private String align;
15         public Rule(String userName, String align) {
16             this.userName = userName;
17             this.align = align;
18         }
19     }
20 
21     @Data
22     class Hero {
23         private String userName;
24         private int bloodVolume;
25         private String align;
26         public Hero(String userName, int bloodVolume, String align) {
27             this.userName = userName;
28             this.bloodVolume = bloodVolume;
29             this.align = align;
30         }
31     }
32 
33     @Test
34     public void test(){
35         List<Person> userListA = Arrays.asList(
36                 new Person("關羽", 926),
37                 new Person("趙雲", 916),
38                 new Person("張飛", 906),
39                 new Person("許褚", 911));
40         List<Rule> userListB = Arrays.asList(
41                 new Rule("關羽", "字·雲長-關公-武財神-漢壽亭侯"),
42                 new Rule("張飛", "字·益德-勇武過人-西鄉侯"),
43                 new Rule("劉備", "字·玄德-百折不撓-漢昭烈帝"),
44                 new Rule("趙雲", "字·子龍-忠義-永昌亭侯"),
45                 new Rule("周瑜", "字·公瑾-文武兼備-永昌亭侯"),
46                 new Rule("許褚", "字·仲康-勇力絕人-虎侯"));
47 
48         List<Hero> commonList = userListA.stream()
49                 .map((uA) -> {
50                     return userListB.stream()
51                             .filter((uB) -> {
52                                 return StringUtils.equals(uB.getUserName(), uA.getUserName());
53                             })
54                             .map((uB) -> {
55                                 return new Hero(uB.getUserName(), uA.getBloodVolume(), uB.align);
56                             })
57                             .collect(Collectors.toList());
58                 }) // 結果類型 Steam<List<Hero>>
59                 .flatMap(List::stream) // 結果類型 Steam<Hero>
60                 .collect(Collectors.toList()); // 結果類型 List<Hero>
61 
62         System.out.println(JSON.toJSONString(commonList)); 
63         // [{"align":"字·雲長-關公-武財神-漢壽亭侯","bloodVolume":926,"userName":"關羽"},
64         // {"align":"字·子龍-忠義-永昌亭侯","bloodVolume":916,"userName":"趙雲"},
65         // {"align":"字·益德-勇武過人-西鄉侯","bloodVolume":906,"userName":"張飛"},
66         // {"align":"字·仲康-勇力絕人-虎侯","bloodVolume":911,"userName":"許褚"}]
67     }

 看到最后,可能發現,不能完全符合現有需求。

 但是,應該能從其中 獲得啟示,因為,有了這個處理,就可以處理其他的。比如 這里的 A、B 可以是不同的對象 及 最終的返回結果  commonList 可以是 對象C 的集合(對象C 數據來源於  A、B對象,或 自定義標識值……)。

 通過 對象的屬性進行比對取值,記得唯一性,如果出現 一對多,或者 多對多,會出現 乘積的效應,也應根據當前的業務進行考慮,是否需要 進行 去重后再做上述操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM