前言
提示:这里可以添加本文要记录的大概内容:
日常工作的记录
提示:以下是本篇文章正文内容,下面案例可供参考
1.如何是的将list中的某一字段进行去重并且取出来呢?
public List<String> getCompanyEnergyType(CompanyDataVo companyDataVo) {
List<ElecCompanyGreenEnergy>list=elecCompanyGreenEnergyService.getCompanyGreenEnergyType(companyDataVo);
List<String> typeList = list.stream()
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ElecCompanyGreenEnergy::getType))))
.stream()
.map(ElecCompanyGreenEnergy::getType).filter(type-> type!= null && !type.isEmpty()).collect(Collectors.toList());
return typeList;
}
主要使用的是collect()与map()其中collect是为了去重,生成的是treeset,之后为了改为list就需要使用map,但是使用map的时候记得还需要stream一下。