List<SlocPlant> all = slocPlantMapper.getAll();
HashMap<String, List<String>> map = new HashMap<>();
//通过Stream流 map是内是得到遍历的对象下的每个属性
Set<String> collect = all.stream()
.map(s -> s.getPlant()) //就是遍历得到每一个plant属性
.collect(Collectors.toSet());//然后放到一个set列表中(去重)
for (String s : collect) {
List<String> slocs = all.stream()
.filter(sloc -> s.equals(sloc.getPlant()))//filter是筛选出符合的条件,相当于if(s.equals(sloc.getPlant())
.map(sp -> sp.getSloc()) //将得到的sloc属性全部放到list中
.collect(Collectors.toList());
map.put(s,slocs);
}
//==========上面内容同下=============
HashSet<String> collect = new HashSet<>();
all.forEach(x->{
collect.add(x.getPlant());
});
for(String s : collect ){
List<String> list = new ArrayList<>();
all.forEach(x->{
if(s.equals(x.getPlant())){
list.add(x.getSloc());
}
});
map.put(s,list);
}
关于stream流遍历的用法
最新推荐文章于 2025-04-30 15:51:06 发布