首页 > 技术文章 > stream流新特性 list转map

fdy-study-consist 2019-12-14 22:54 原文

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

推荐阅读