对list集合以集合元素中某属性分组:
//通过标签ID分组
Map<String, List<LabelTpl>> groupLabels =
labelTpls.stream().collect(Collectors.groupingBy(LabelTpl::getCpsLabelNo));
将list转换为map
Map<String,String> outMap = labelList
.stream().filter(d -> d.getCpsLabelNo() != null && d.getCpsOutNodeNo() != null)
.collect(Collectors.toMap(Label::getCpsLabelNo, Label::getCpsOutNodeNo, (key1, key2) -> key2, HashMap::new));
循环list
List<FileLabelTpl> allTpl;
allTpl.stream().forEach(fileLabelTpl -> {
tplMap.put(fileLabelTpl.getCpsOutNodeNo(), fileLabelTpl.getCpsTplNodeName());
});
以集合中某字段去重
List<FileLabelTpl> fileLabelTplList = fileLabelTplList1.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(FileLabelTpl::getCpsOutNodeNo))), ArrayList::new));