本篇内容介绍了“java8的collectors怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联公司是专业的文圣网站建设公司,文圣接单;提供成都做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行文圣网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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"); Listlist = Arrays.asList(a1, a2, a3, a4, a5, a6);
基础的toList、toSet、toMap就不讲了,简单易用,一看就会,我说说稍微复杂的。
collectingAndThen:使用collector进行归集,然后对归结的结果进行加工
Listcollect = 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> 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 > 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 > 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 > 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> 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 > 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用法一致:使用一个比较器获得最大值或最小值
Optionalmin = list.stream().map(KeyAndValue::getName).min(Comparator.comparing(v->v.length())); System.out.println(min.get());//kevinOptional min1 = list.stream().min(Comparator.comparing(v->v.getValue().length())); System.out.println(min1.get());// KeyAndValue(name=kevin, value=lee)Optional min2 = list.stream().map(KeyAndValue::getName).collect(Collectors.minBy(Comparator.comparing(String::length))); System.out.println(min2.get());//kevinOptional 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进行收集
Listmapping = list.stream().collect(Collectors.mapping(v -> v.getValue().concat("agc"), Collectors.toList())); System.out.println(mapping);// 类似 Stream 先进行map操作,再进行collectList 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()
ArrayListcollect1 = 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)]
根据某个属性进行去重
TreeSetcollect = list.stream().collect(Collectors.toCollection(() -> new TreeSet (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怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!