您好,登录后才能下订单哦!
本篇内容介绍了“java8的collectors怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
java8中提供了对集合进行转化,然后进行归集的的操作。这就不得不提Collectors这个类了,真的很强大,我们看示例吧。
KeyAndValue a1 = new KeyAndValue().setName("kevin").setValue("lee"); KeyAndValue a2 = new KeyAndValue().setName("kevin").setValue("lee"); KeyAndValue a3 = new KeyAndValue().setName("kevin1").setValue("lee"); KeyAndValue a4 = new KeyAndValue().setName("kevin1").setValue("123123"); KeyAndValue a5 = new KeyAndValue().setName("kevin2").setValue("lee5"); KeyAndValue a6 = new KeyAndValue().setName("kevin3").setValue("lee8"); List<KeyAndValue> list = Arrays.asList(a1, a2, a3, a4, a5, a6);
基础的toList、toSet、toMap就不讲了,简单易用,一看就会,我说说稍微复杂的。
collectingAndThen:使用collector进行归集,然后对归结的结果进行加工
List<String> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.mapping(a -> a.getValue(), Collectors.toList()), v -> { v.sort(Comparator.comparing(String::length).reversed());return v; })); System.out.println(collect);//[123123, lee5, lee8, lee, lee, lee]
groupingBy 分组操作:基于一个key值,对元素进行分组
Map<String, List<KeyAndValue>> collect = list.stream().collect(Collectors.groupingBy(a -> a.getName())); System.out.println(JSON.toJSONString(collect));// {"kevin":[{"name":"kevin","value":"lee"},{"name":"kevin","value":"lee"}],"kevin2":[{"name":"kevin2","value":"lee5"}],"kevin1":[{"name":"kevin1","value":"lee"},{"name":"kevin1","value":"123123"}],"kevin3":[{"name":"kevin3","value":"lee8"}]}Map<String, Set<String>> collect1 = list.stream().collect(Collectors.groupingBy(KeyAndValue::getName, Collectors.mapping(b -> b.getValue(), Collectors.toSet()))); System.out.println(JSON.toJSONString(collect1));// {"kevin":["lee"],"kevin2":["lee5"],"kevin1":["123123","lee"],"kevin3":["lee8"]}Map<String, Set<KeyAndValue>> collect3 = list.stream().collect(Collectors.groupingBy(KeyAndValue::getName, Collectors.toSet())); System.out.println(JSON.toJSONString(collect3));// {"kevin":[{"name":"kevin","value":"lee"}],"kevin2":[{"name":"kevin2","value":"lee5"}],"kevin1":[{"name":"kevin1","value":"lee"},{"name":"kevin1","value":"123123"}],"kevin3":[{"name":"kevin3","value":"lee8"}]}Map<String, Set<KeyAndValue>> collect4 = list.stream().collect(Collectors.groupingBy(KeyAndValue::getName,TreeMap::new, Collectors.toSet())); System.out.println(JSON.toJSONString(collect4));// {"kevin":[{"name":"kevin","value":"lee"}],"kevin1":[{"name":"kevin1","value":"lee"},{"name":"kevin1","value":"123123"}],"kevin2":[{"name":"kevin2","value":"lee5"}],"kevin3":[{"name":"kevin3","value":"lee8"}]}
join 操作:对元素进行拼接,有三个方法。
String joinResult = list.stream().map(KeyAndValue::getName).collect(Collectors.joining()); System.out.println(joinResult);//kevinkevinkevin1kevin1kevin2kevin3String joinResult1 = list.stream().map(KeyAndValue::getName).collect(Collectors.joining(",")); System.out.println(joinResult1);// kevin,kevin,kevin1,kevin1,kevin2,kevin3String joinResult2 = list.stream().map(KeyAndValue::getName).collect(Collectors.joining(",","(",")")); System.out.println(joinResult2);// (kevin,kevin,kevin1,kevin1,kevin2,kevin3)
partitioningBy操作:基于断言对元素按true和false进行分组
Map<Boolean, List<KeyAndValue>> partitioningBy = list.stream() .collect(Collectors.partitioningBy(k -> k.getName().equals("kevin"))); System.out.println(partitioningBy);//{false=[KeyAndValue(name=kevin1, value=lee), KeyAndValue(name=kevin1, value=123123), KeyAndValue(name=kevin2, value=lee5), KeyAndValue(name=kevin3, value=lee8)], true=[KeyAndValue(name=kevin, value=lee), KeyAndValue(name=kevin, value=lee)]}Map<Boolean, List<String>> partitioningBy1 = list.stream() .collect(Collectors.partitioningBy(k -> k.getName().equals("kevin"), Collectors.mapping(v -> v.getValue(), Collectors.toList()))); System.out.println(partitioningBy1);// {false=[lee, 123123, lee5, lee8], true=[lee, lee]}
min/minBy , max/maxBy 用法一致:使用一个比较器获得最大值或最小值
Optional<String> min = list.stream().map(KeyAndValue::getName).min(Comparator.comparing(v->v.length())); System.out.println(min.get());//kevinOptional<KeyAndValue> min1 = list.stream().min(Comparator.comparing(v->v.getValue().length())); System.out.println(min1.get());// KeyAndValue(name=kevin, value=lee)Optional<String> min2 = list.stream().map(KeyAndValue::getName).collect(Collectors.minBy(Comparator.comparing(String::length))); System.out.println(min2.get());//kevinOptional<KeyAndValue> min3 = list.stream().collect(Collectors.minBy(Comparator.comparing(v->v.getValue().length()))); System.out.println(min3.get());// KeyAndValue(name=kevin, value=lee)
mapping操作:使用一个function对元素进行加工,然后使用collector进行收集
List<String> mapping = list.stream().collect(Collectors.mapping(v -> v.getValue().concat("agc"), Collectors.toList())); System.out.println(mapping);// 类似 Stream 先进行map操作,再进行collectList<String> mapping_ = list.stream().map(v -> v.getValue().concat("agc")).collect(Collectors.toList()); System.out.println(mapping_);// [leeagc, leeagc, leeagc, 123123agc, lee5agc, lee8agc]
toCollection:将元素按照顺序复制到另一个收集器(集合)中,类似于Collectors.toList()
ArrayList<KeyAndValue> collect1 = list.stream().collect(Collectors.toCollection(ArrayList::new)); System.out.println(collect1);// [KeyAndValue(name=kevin, value=lee), KeyAndValue(name=kevin, value=lee), KeyAndValue(name=kevin1, value=lee), KeyAndValue(name=kevin1, value=123123), KeyAndValue(name=kevin2, value=lee5), KeyAndValue(name=kevin3, value=lee8)]
根据某个属性进行去重
TreeSet<KeyAndValue> collect = list.stream().collect(Collectors.toCollection(() -> new TreeSet<KeyAndValue>(Comparator.comparing(KeyAndValue::getName)))); System.out.println(collect);// [KeyAndValue(name=kevin, value=lee), KeyAndValue(name=kevin1, value=lee), KeyAndValue(name=kevin2, value=lee5), KeyAndValue(name=kevin3, value=lee8)]
“java8的collectors怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。