您好,登錄后才能下訂單哦!
本篇內容介紹了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。