map 和 list 都数据 集合的一种
map 是 键值对的集合 类型
list 是 多个数据的集合
// map() 抽 集合里的 某个key 然后 放到集合里
// filter() 过滤 条件
// map 取出集合里的某个key 放到 新集合里
// 方法一
List<Integer> sellerIds = orderCarts.stream().map(i -> Integer.valueOf(i.get("sellerId") + ""))
.distinct().collect(Collectors.toList());
//方法二
List<Integer> enquiryOrderIds = stationEntryExitDetails.stream()
.map(ServiceStationEntryExitDetail::getEnquiryOrderId)
.distinct().collect(Collectors.toList());
// 把list 里的 所有数据 抽出来 组成 map 当 key 的值 不能重复
//根据集合里的某个值 当map 的key 集合里的 某个值 当map里的值 变成新的map
Map<Integer, String> collect = miniappUsers.stream()
.collect(Collectors.toMap(MiniappUser::getId, MiniappUser::getAvatar));
// 根据集合里的某个值当 map 的key 把整个 list的值 重新赋给 map 里的值 变成新的map
Map<Integer, BackendOrderDetail> backendOrderDetailMap = backendOrderDetails.stream()
.collect(Collectors.toMap(BackendOrderDetail::getId, b -> b));
//根据集合里的某个值 进行 分组 组成一个新的 map
Map<Integer, List<ServiceStationEntryExit>> entryExitMap = response.getStationEntryExits()
.stream().collect(Collectors.groupingBy(ServiceStationEntryExit::getFactoryId));
// 根据list里的某个值 进行总和计算
Integer numbers = statementOrders.stream().map(StatementOrder :: getNumber).reduce(Integer::sum).get();
// 根据list里的某个值 进行 加的计算
BigDecimal amounts = statementOrders.stream().map(StatementOrder :: getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
根据某个 值当key 抽取某个值 为list
Map<String, List<String>> collect = ysFormUserRestrictList.stream()
.collect(Collectors.groupingBy(YsFormUserRestrict::getFormTableCode,
Collectors.mapping(YsFormUserRestrict::getField, Collectors.toList())));
按照类目分组:
Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory));
//{"啤酒":[{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"零食":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20},{"category":"零食","id":3,"name":"月饼","num":3,"price":30}]}
按照几个属性拼接分组:
Map<String, List<Product>> prodMap = prodList.stream().collect(Collectors.groupingBy(item -> item.getCategory() + "_" + item.getName()));
//{"零食_月饼":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30}],"零食_面包":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5}],"啤酒_百威啤酒":[{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"啤酒_青岛啤酒":[{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10}],"零食_饼干":[{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}
根据不同条件分组
Map<String, List<Product>> prodMap= prodList.stream().collect(Collectors.groupingBy(item -> {
if(item.getNum() < 3) {
return "3";
}else {
return "other";
}
}));
//{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30},{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}
多级分组
要实现多级分组,我们可以使用一个由双参数版本的Collectors.groupingBy工厂方法创 建的收集器,它除了普通的分类函数之外,还可以接受collector类型的第二个参数。那么要进 行二级分组的话,我们可以把一个内层groupingBy传递给外层groupingBy,并定义一个为流 中项目分类的二级标准。
Map<String, Map<String, List<Product>>> prodMap= prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.groupingBy(item -> {
if(item.getNum() < 3) {
return "3";
}else {
return "other";
}
})));
//{"啤酒":{"other":[{"category":"啤酒","id":4,"name":"青岛啤酒","num":3,"price":10},{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15}]},"零食":{"other":[{"category":"零食","id":3,"name":"月饼","num":3,"price":30}],"3":[{"category":"零食","id":1,"name":"面包","num":1,"price":15.5},{"category":"零食","id":2,"name":"饼干","num":2,"price":20}]}}
按子组收集数据
求总数
Map<String, Long> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.counting()));
//{"啤酒":2,"零食":3}
求和
Map<String, Integer> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.summingInt(Product::getNum)));
//{"啤酒":13,"零食":6}
把收集器的结果转换为另一种类型
Map<String, Product> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Product::getNum)), Optional::get)));
//{"啤酒":{"category":"啤酒","id":5,"name":"百威啤酒","num":10,"price":15},"零食":{"category":"零食","id":3,"name":"月饼","num":3,"price":30}}
联合其他收集器
Map<String, Set<String>> prodMap = prodList.stream().collect(Collectors.groupingBy(Product::getCategory, Collectors.mapping(Product::getName, Collectors.toSet())));
//{"啤酒":["青岛啤酒","百威啤酒"],"零食":["面包","饼干","月饼"]}