//stream对象转map, 如果存在多value则取第一个
Map collect = proeducts.stream().collect(Collectors.toMap(Proeduct::getValue, Function.identity(), (v1, v2) -> v1));
collect.forEach((aDouble, proeduct) -> System.out.println(aDouble.toString() + " " + proeduct.toString()));
//stream对象转map
Map collect1 = proeducts.stream().collect(Collectors.toMap(Proeduct::getValue, Function.identity()));
collect1.forEach((aDouble, proeduct) -> System.out.println(aDouble.toString() + " " + proeduct.toString()));
Map studentMap = studentList.stream().collect(Collectors.toMap(key -> { return key.getId() + "-" + key.getName(); }, student -> student));
//stream对象转map
Map> stringListMap = studentList.stream().collect(Collectors.groupingBy(st -> {
return st.getId() + "";
}, Collectors.toList()));
//stream对象转map
Map> collect = studentList.stream().collect(Collectors.groupingBy(st -> {
return st.getName() + "-" + st.getAge();
}, LinkedHashMap::new, Collectors.toList()));
//stream对象转map集合
Map> map = stringList.stream().collect(Collectors.groupingBy(s -> CommonUtils.isNotEmpty(s)));
List stringList1 = map.get(Boolean.FALSE);
System.out.println(stringList1.size());
//stream对象转map集合
Map> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory,
Collectors.mapping(Product::getName, Collectors.toSet())));
//{"啤酒":["青岛啤酒","百威啤酒"],"零食":["面包","饼干","月饼"]}
Map stringMap = studentDTOList.stream().collect(Collectors.toMap(StudentDTO::getId, StudentDTO::getName));
stringMap.keySet().forEach(
key -> {
System.out.println(key + " : " + stringMap.get(key));
}
);
//stream对象字段转字符串
String collect2 = proeducts.stream().filter(p -> !p.getId().isEmpty()).map(proeduct -> proeduct.getId()).collect(Collectors.joining(","));
System.out.println(collect2);
//分组获得组内最小值
Map> collect = proeducts.stream().collect(
Collectors.groupingBy(
Proeduct::getName,
Collectors.minBy(Comparator.comparing(Proeduct::getValue))
));
//> 分组后只获取对象中某一个属性作为一个集合
Map> collectMap = studentList.stream().collect(
Collectors.groupingBy(Student::getAge, //key
Collectors.mapping(Student::getId, //value需要的字段
Collectors.mapping(String::valueOf, //value转换类型
Collectors.toList() //值转为集合
))));
// 分组后获取对象中某一个属性进行求和
Map mapKey = studentList.stream().collect(
Collectors.groupingBy(Student::getAge, //分组key
Collectors.mapping(Student::getId, //求和字段
Collectors.mapping(String::valueOf, //求和字段转换为字符串
Collectors.mapping(BigDecimal::new, //字符串转为BigDecimal类型
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add) //求和操作
)))));
// stream 流的使用
String s = stringList.stream() //转为 Stream 流
.filter(i -> !isNum(i)) //筛选字母型字符串
.filter(i -> i.length() >= 2) //字符串长度 大于 2
.map(i -> i.toLowerCase()) //全部转换为小写
.distinct() //去重操作
.sorted(Comparator.naturalOrder()) //字符串排序
.collect(Collectors.joining(" "));
consumer.accept("--------->>>>>>" + s);
中间结点(stream集合) 和 终止结点(非stream集合)
appleList.stream()
.filter(apple -> apple.getColor().equals("read") || apple.getColor().equals("green")) //过滤
.map(apple -> apple.getWeight()) //转换
.filter(w -> w > 300) //过滤
.peek(w -> System.out.println("重量是:" + w)) //执行一个函数 peek 是一个中间节点
.collect(Collectors.toList()) // 终止
.forEach(w -> System.out.println(w)); // 执行一个函数 forEach 是一个终止节点 遍历