个人最近常使用集合lamda,大致整理下
-
Map的values快速转List
Map<String,Obj> diagnosisInvoiceMap = new ConcurrentHashMap<>();
List list = diagnosisInvoiceMap.values().stream().collect(Collectors.toList(); -
集合的对象某个字段数据转速转Set
List dataList = new ArrayList();
Set problemSet = dataList.stream().map(e->e.getFile_id()).collect(Collectors.toSet()); -
集合数据过滤
List detaillist = costDetailDtoList.stream().filter(e -> e.getInvoiceNo().equals(hospitalInvoice.getInvoiceNo()) ).collect(Collectors.toList()); -
判断集合是否过滤到数据(orelse)
(1)第一种方式
List dataList = new ArrayList();
Optional first = dataList ().stream().filter(e -> e.getDrug_name().equals(costDetailDto.getProjectName())).findFirst();
if(null == first || !first.isPresent()){
//没有数据
continue;
}
drugListDto = first.get();
(2)第二种方式
List dataList = new ArrayList();
Stream.of(dataList.values()).filter(e -> e.getType().equals(type)).findFirst().orElse(null);
(3). 第三种方式
booolean flag = dataList ().stream().anyMach(data->data.equals(“3”)); //另:allMach 意思全部是否等于3,noneMatch正好相反
为ture为全匹配上 -
集合数据,转Map:按照集合的某一个属性作为key,把对象作为对象
(1)方法1
List dataList = new ArrayList();
Map<String, Obj> invoiceMap = dataList.collect(Collectors.toMap(paramCalcrate -> paramCalcrate.getInvoiceNo(), Function.identity()));
(2)方法2
Map<String, String> attrMap = dataList.stream().collect(Collectors.toMap(Obj::getName, Obj::getCity));
(3) 方法3
Map<String, Person> mapByName = persons.stream().collect(Collectors.toMap(Person::getName, p -> p)); -
集合对象的对象某个字段快速这个字段的集合
List dataList = new ArrayList();
List diagCodeList = dataList.map(e->e.getDiagnosisName()).filter(StringUtils::isNotBlank).collect(Collectors.toList()); -
集合对象的金额属性统计汇总
List dataList = new ArrayList();
BigDecimal payAmountSum = dataList.map(AdjustAndPerson::getArealAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
另:dataList.stream().mapToInt(o -> Objects.isNull(o.getAmount()) ? 0 : o.getAmount()).sum()) -
集合对象的对象属性字段的以“,”分割
List dataList = new ArrayList();
String content = dataList.stream().map(x -> x.getProjectName() + “|” + x.getAmount()).collect(Collectors.joining(“,”)); -
.集合按照集合对象某个属性分组group
(1) 方法1
List dataList = new ArrayList();
Map<String, List> groupBySex = dataList.collect(Collectors.groupingBy(Obj::getItemName));
(2) 方法2,自定义
Map<Object, List> cityGroupSelf = dataList .stream()
.collect(Collectors.groupingBy(obj -> {
if (“北京”.equals(person.getCity()) || “广州”.equals(person.getCity())) {
return “北广”;
} else {
return person.getCity();
}
}));
}); -
集合的某个字段排序
(1)方法1
List dataList = new ArrayList();
List result = dataList.values().stream().sorted(
// 优先1
Comparator.comparing(GetDuty::getDutyType, Comparator.reverseOrder())
// 优先2
.thenComparing(GetDuty::getDutyClass)
// 优先3
.thenComparing(GetDuty::getTreatType, Comparator.reverseOrder());
(2)方法2
List personsSorted = dataList .stream().sorted((a, b) -> {
return (int) (a.getBirthday().getTime() - b.getBirthday().getTime());
}).collect(Collectors.toList()); -
集合遍历
List dataList = new ArrayList();
dataList.forEach(person -> {
person.setName(person.getName() + “@”);
}); -
属性去重
(1)方法1
List dataList = new ArrayList();
List names = dataList.stream().map(p->p.getName()).collect(Collectors.toList());
System.out.println(“list的属性列表”+names);
System.out.println(“去重后的列表:”+names.stream().distinct().collect(Collectors.toList()));
(2)方法2
TreeSet set = new TreeSet<>(dataList );
System.out.println(“去重集合:” + set); -
集合数据取最大值
List dataList = new ArrayList(“1”,“2”,“3”);
int maxValue = dataList.stream().max(Comparator.comparing(Integer,parseInt)).get(); -
排序
List result = standardMedicalCatalogDtoList.stream().sorted(
Comparator.comparing(BStandardMedicalCatalogDto::getCoincidentWordScore, Comparator.reverseOrder())
.thenComparing(BStandardMedicalCatalogDto::getDrugClassSort)
.thenComparing(BStandardMedicalCatalogDto::getMedicalCategory)
).collect(Collectors.toList());
转参考资料:https://blog.csdn.net/qq_31635851/article/details/120322776