1. 根据实体属性分组,重复的放在同一个list里边,可用于去重或者对同一个分组里边数据操作
List<Bean> input = new ArrayList<>();
Map<String, List<Bean>> mapList =input.stream().collect(Collectors.groupingBy(Bean::getLabel));
2.遍历
List<Bean>.stream().forEach(item -> {
});
3.list转map
Map<Long, Bean> dataFieldMap =List<Bean>.stream().collect(Collectors.toMap(Bean::getId, f -> f));
重复key时取后面的key
Map<Long,Long> relationMap = relationList.stream().collect(Collectors.toMap(Bean::getId,Bean::getFieldId,(key1,key2)->key2));
当map的value是个集合时
Map<Long, Set<Long>> refObjectMap = relationEntities.stream().collect(Collectors.toMap(DataRelationEntity::getRefObjectId,
p -> {
Set<Long> itemList = new HashSet<>();
itemList.add(p.getObjectId());
return itemList;
},
(Set<Long> value1, Set<Long> value2) -> {
value1.addAll(value2);
return value1;
}));
4.list转set
List<Bean> records = new ArrayList<>();
Set<Long> appIds =records .stream().map((bean) -> bean.getId()).collect(Collectors.toSet());
5.先去重,再过滤,最后转成实体list
List<Bean> records = new ArrayList<>();
List<BeanA> insertPageList = records .stream().distinct().filter(Objects::nonNull).map(r -> {
BeanA = new BeanA();
p.setRelationId(r);
return p;
}).collect(Collectors.toList());
6.过滤
List<Map<String, Object>> result =。。。
List<Map<String, Object>> data = result.stream().filter(t -> "hello world".indexOf(String.valueOf(t.get("type"))) < 0).collect(Collectors.toList());
7.获取实体属性
List<Bean> result =。。。;
List<Long> fields = result.stream().map(Bean::getFieldId)
.collect(Collectors.toList());
8.去重统计
long codeCount = options.stream().map(Option::getCode).collect(Collectors.toList()).stream().distinct().count();
更多学习可以查看java.util.stream中的方法