大家好,欢迎来到IT知识分享网。
1、List<String> 分组
package com.mkyong.java8; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Java8Example1 { public static void main(String[] args) { //3 apple, 2 banana, others 1 List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya"); Map<String, Long> result = items.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); System.out.println(result); } }
2、List<String> 分组并排序
package com.mkyong.java8; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Java8Example2 { public static void main(String[] args) { //3 apple, 2 banana, others 1 List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya"); Map<String, Long> result = items.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); Map<String, Long> finalMap = new LinkedHashMap<>(); //Sort a map and add to finalMap result.entrySet().stream() .sorted(Map.Entry.<String, Long>comparingByValue() .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue())); System.out.println(finalMap); } }
3、List<Object> 分组并排序
pojo
package com.mkyong.java8; import java.math.BigDecimal; public class Item { private String name; private int qty; private BigDecimal price; }
分组并排序
package com.mkyong.java8; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Java8Examples3 { public static void main(String[] args) { //3 apple, 2 banana, others 1 List<Item> items = Arrays.asList( new Item("apple", 10, new BigDecimal("9.99")), new Item("banana", 20, new BigDecimal("19.99")), new Item("orang", 10, new BigDecimal("29.99")), new Item("watermelon", 10, new BigDecimal("29.99")), new Item("papaya", 20, new BigDecimal("9.99")), new Item("apple", 10, new BigDecimal("9.99")), new Item("banana", 10, new BigDecimal("19.99")), new Item("apple", 20, new BigDecimal("9.99")) ); Map<String, Long> counting = items.stream().collect( Collectors.groupingBy(Item::getName, Collectors.counting())); System.out.println(counting); Map<String, Integer> sum = items.stream().collect( Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty))); System.out.println(sum); } }
4、根据价格分组
package com.mkyong.java8; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; public class Java8Examples4 { public static void main(String[] args) { //3 apple, 2 banana, others 1 List<Item> items = Arrays.asList( new Item("apple", 10, new BigDecimal("9.99")), new Item("banana", 20, new BigDecimal("19.99")), new Item("orang", 10, new BigDecimal("29.99")), new Item("watermelon", 10, new BigDecimal("29.99")), new Item("papaya", 20, new BigDecimal("9.99")), new Item("apple", 10, new BigDecimal("9.99")), new Item("banana", 10, new BigDecimal("19.99")), new Item("apple", 20, new BigDecimal("9.99")) ); //group by price Map<BigDecimal, List<Item>> groupByPriceMap = items.stream().collect(Collectors.groupingBy(Item::getPrice)); System.out.println(groupByPriceMap); // group by price, uses 'mapping' to convert List<Item> to Set<String> Map<BigDecimal, Set<String>> result = items.stream().collect( Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet()) ) ); System.out.println(result); } }
5、GrouyBy
Map<Integer, Integer> totalNoThings = somethings.stream()
.collect(Collectors.groupingBy(Something::getParentKey,
Collectors.summingInt(Something::getNoThings)));
6、二次排序
List<Something> sorted=somethings.stream().sorted( Comparator.comparing((Something x)->totalNoThings.get(x.getParentKey())) .thenComparing(Something::getNoThings).reversed()) .collect(Collectors.toList());
7、通过多条件分组
Map<List<String>, Integer> map = myList.stream() .collect(Collectors.groupingBy( f -> Arrays.asList(f.getType(), f.getCode()), Collectors.summingInt(Foo::getQuantity) )); List<Foo> result = map.entrySet() .stream() .map(e -> new Foo(e.getKey().get(0), e.getValue(), e.getKey().get(1))) .collect(Collectors.toList());
8、高级写法
List<Foo> result = new ArrayList<>( myList.stream() .collect(Collectors.groupingBy( f -> Arrays.<String>asList(f.getType(), f.getCode()), MoreCollectors.pairing( Collectors.collectingAndThen(MoreCollectors.first(), Optional::get), Collectors.summingInt(Foo::getQuantity), (f, s) -> new Foo(f.getType(), s, f.getCode()) ) )).values());
参考:https://blog.csdn.net/frankenjoy123/article/details/70739800
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/30185.html