stream流新特性 list轉map


   @Test
    public void test01(){
        List<TbSeller> tbSellers = tbSellerMapper.selectAll();
        for (TbSeller tbSeller : tbSellers) {
            System.out.println(tbSeller);
        }
// (k1, k2) -> k1)是為了解決TbSeller::getSellerId作為key時,會報異常 Function.identity()的結果是每個實例對象TbSeller System.out.println("******************list轉map key為某一字段 value為對象*****************"); Map<String, TbSeller> tbSellerMap = tbSellers.stream().collect(Collectors.toMap(TbSeller::getSellerId, Function.identity(), (k1, k2) -> k1)); System.out.println("tbSellers轉成map的結果tbSellerMap是:"+JSON.toJSONString(tbSellerMap)); // { //"baidu" :{ "addressDetail" : "西二旗小胡同", // "linkmanMobile" : "1390000111", // "linkmanName" : "李彥宏", // "linkmanQq" : "123456", // "name" : "百度公司", // "nickName" : "百度商店", // "password" : "123456", // "sellerId" : "baidu", // "status" : "1", // "telephone" : "4004004400" }, // //"baima" :{ "name" : "我也不知道", // "nickName" : "你懂的", // "password" : "123456", // "sellerId" : "baima", // "status" : "1", // "telephone" : "13388888899" }}
System.out.println("***************list過濾********************"); // 基本數據類型用== 進行比較 其他大部分用equals進行比較 List<TbSeller> newTbSellerList = tbSellers.stream().filter(tbSeller -> StringUtils.equals(tbSeller.getSellerId(),"baidu")) .collect(Collectors.toList()); System.out.println("tbSellers過濾的結果newTbSellerList是:"+JSON.toJSONString(newTbSellerList)); /** [{ "addressDetail" : "西二旗小胡同", "linkmanMobile" : "1390000111", "linkmanName" : "李彥宏", "linkmanQq" : "123456", "name" : "百度公司", "nickName" : "百度商店", "password" : "123456", "sellerId" : "baidu", "status" : "1", "telephone" : "4004004400" }] */
System.out.println("***************分組,計數********************"); Map<String, Long> collect = tbSellers.stream().collect(Collectors.groupingBy(TbSeller::getName, Collectors.counting())); System.out.println("tbSellers過濾的結果collect是:"+JSON.toJSONString(collect)); // 復雜寫法 Map<TbSeller, Long> collect1 = tbSellers.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println("tbSellers過濾的結果collect1是:"+JSON.toJSONString(collect1)); /** {"我也不知道":1,"百度公司":1} */

System.out.println("***************list<對象>轉list<單個字段>********************");
List<Long> collect2 = tbSellers.stream().map(TbSeller::getAddress).collect(Collectors.toList());
System.out.println(collect2);
/**
[null, null]
*/
System.out.println("**************list去重*********************");
List<String> listA = new ArrayList<>();
listA.add("aa");
listA.add("aa");
listA.add("bb");
listA.add("aa");
List<String> listB = new ArrayList<>();
listB.add("bb");
listB.add("bb");
listB.add("cc");
listB.add("ee");
listB.add("ff");
// 多個list
List<String> collect3 = Stream.of(listA, listB).flatMap(Collection::stream).distinct().collect(Collectors.toList());
System.out.println(collect3);
// 結果: [aa, bb, cc, ee, ff]
/*****************************************************/
// 多個Stream
List<String> collect4 = Stream.of(listA.stream(), listB.stream()).flatMap(Function.identity()).distinct().collect(Collectors.toList());
System.out.println(collect4);
// 結果: [aa, bb, cc, ee, ff]

System.out.println(Stream.of(1, 2, 3, 1, 2).distinct().collect(Collectors.toList()));
// 結果: [1, 2, 3]
Person p1 = new Person("a",1, "a");
Person p2 = new Person("b",2, "b");
Person p3 = new Person("a",3, "a");
Person p4 = new Person("c",1, "c");
Person p5 = new Person("a",1, "a");
// 將list轉換為Stream,調用distinct方法,然后再講Stream轉換為list
System.out.println(Stream.of(p1, p2, p3, p4, p5).distinct().collect(Collectors.toList()));
// 輸出結果為:
// [Person(name=a, age=1, nation=a), Person(name=b, age=2, nation=b), Person(name=a, age=3, nation=a), Person(name=c, age=1, nation=c)]
}
/**
* 有很多任務不清楚是使用流還是迭代。例如,考慮初始化一副新牌的任務。假設 Card 是一個不可變的值類,它封裝了 RankSuit
* 它們都是枚舉類型。這個任務代表任何需要計算可以從兩個集合中選擇的所有元素對。數學家們稱它為兩個集合的笛卡爾積。
* 下面是一個迭代實現,它有一個嵌套的 for-each 循環,你應該非常熟悉:

*/
// Iterative Cartesian product computation
private static List<Card> newDeck() {
List<Card> result = new ArrayList<>();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
result.add(new Card(rank, suit));
}
}
return result;
}

// Stream-based Cartesian product computation
private static List<Card> newDeck2() {
Rank[] ranks = Rank.values();
// flatMap()多個流 map是將list轉為map的鍵值對
return Stream.of(Suit.values())
.flatMap(suit -> Stream.of(ranks).map(rank -> new Card(rank, suit)))
.collect(toList());
}
 


免責聲明!

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



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