//List 以名称分组 Map>
public static void testGroupBy1(List users) {
Map> groupByMap = users.stream().collect(Collectors.groupingBy(User::getName));
for (Map.Entry> entry : groupByMap.entrySet()) {
System.out.println("分组对应的key:"+ entry.getKey() +";对应的value:"+ entry.getValue() +";");
}
// 分组对应的key:lisi;对应的value:[User{id=2, name='lisi', age=19, grade=50.0}];
// 分组对应的key:zhangsan;对应的value:[User{id=1, name='zhangsan', age=20, grade=60.0}, User{id=5, name='zhangsan', age=20, grade=70.0}];
// 分组对应的key:wangwu;对应的value:[User{id=3, name='wangwu', age=20, grade=60.0}, User{id=4, name='wangwu', age=20, grade=70.0}];
}
//List 以名称年龄 Map>
public static void testGroupBy2(List users) {
Map> groupByMap = users.stream().collect(Collectors.groupingBy(b -> b.getAge()));
for (Map.Entry> entry : groupByMap.entrySet()) {
System.out.println("分组对应的key:"+ entry.getKey() +";对应的value:"+ entry.getValue() +";");
}
// 分组对应的key:19;对应的value:[User{id=2, name='lisi', age=19, grade=50.0}];
// 分组对应的key:20;对应的value:[User{id=1, name='zhangsan', age=20, grade=60.0}, User{id=3, name='wangwu', age=20, grade=60.0}, User{id=4, name='wangwu', age=20, grade=70.0}, User{id=5, name='zhangsan', age=20, grade=70.0}];
}
public static void testList2Map1(List users) {
Map maps = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
for (Map.Entry entry : maps.entrySet()) {
System.out.println("分组对应的key:"+ entry.getKey() +";对应的value:"+ entry.getValue() +";");
}
// 分组对应的key:1;对应的value:User{id=1, name='zhangsan', age=20, grade=60.0};
// 分组对应的key:2;对应的value:User{id=2, name='lisi', age=19, grade=50.0};
// 分组对应的key:3;对应的value:User{id=3, name='wangwu', age=20, grade=60.0};
// 分组对应的key:4;对应的value:User{id=4, name='wangwu', age=20, grade=70.0};
// 分组对应的key:5;对应的value:User{id=5, name='zhangsan', age=20, grade=70.0};
}
public static void testList2Map2(List users) {
Map maps = users.stream().collect(Collectors.toMap(User::getName, a -> a,(k1,k2)->k1));
for (Map.Entry entry : maps.entrySet()) {
System.out.println("分组对应的key:"+ entry.getKey() +";对应的value:"+ entry.getValue() +";");
}
// 分组对应的key:lisi;对应的value:User{id=2, name='lisi', age=19, grade=50.0};
// 分组对应的key:zhangsan;对应的value:User{id=1, name='zhangsan', age=20, grade=60.0};
// 分组对应的key:wangwu;对应的value:User{id=3, name='wangwu', age=20, grade=60.0};
}
public static void testFilter(List users) {
List filterList = users.stream().filter(a -> a.getName().equals("zhangsan")).collect(Collectors.toList());
for(User filter : filterList){
System.out.println("过滤出来的集合:"+filter);
}
// 过滤出来的集合:User{id=1, name='zhangsan', age=20, grade=60.0}
// 过滤出来的集合:User{id=5, name='zhangsan', age=20, grade=70.0}
}
//计算张三总分
public static void testSum(List users) {
List filterList = users.stream().filter(a -> a.getName().equals("zhangsan")).collect(Collectors.toList());
Double gradeSum = filterList.stream().map(User::getGrade).reduce((result, item) -> result + item).get();
System.out.println("总和:"+gradeSum);
// BigDecimal 类型的求和
/*BigDecimal amount = list.stream()
// 将对象的mongey取出来map为Bigdecimal
.map(b -> b.getAmount())
// 使用reduce聚合函数,实现累加器
.reduce(BigDecimal.ZERO, BigDecimal::add);*/
}
List list= users.stream()
.filter(b -> "zhangsan".equals(b.getName()))
//.filter(b -> b.getGrade() == 60 )
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(b -> b.getId()))), ArrayList::new));
// 过滤name等于zhangsan,
// 去重->将过滤后的转set,key为id(set集合key不能重复)->在转为ArrayList
Optional maxDish = users.stream().
collect(Collectors.maxBy(Comparator.comparing(User::getAge)));
maxDish.ifPresent(System.out::println);
Optional minDish = users.stream().
collect(Collectors.minBy(Comparator.comparing(User::getAge)));
minDish.ifPresent(System.out::println);
String tenantIds = “1,2,3,4,5,6”;
List listIds = Arrays.asList(tenantIds.split(",")).stream().map(s -> Long.parseLong(s)).collect(Collectors.toList());
Map maps = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
List userList = maps.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
List userIds = users.stream().map(user -> user.getId()).collect(Collectors.toList());
// 以,间隔把List转成String
Joiner.on(",").join(userIds);
Set set = new HashSet<>(listA);
set.addAll(listB);
List listAll = new ArrayList();
listAll.addAll(list1);
listAll.addAll(list2);
listAll = new ArrayList(new LinkedHashSet<>(listAll));