标签:简洁,return,树结构,List,allList,生成,item,collect From: https://www.cnblogs.com/sensenh/p/17495493.html
@Override
public List<DeviceCategoryDictEntity> getDeviceCategoryTree() {
//查询设备类型字典表的所有数据
List<DeviceCategoryDictEntity> allList = deviceCategoryDictDao.selectList(new QueryWrapper<>());
//生成树结构
return allList.stream().filter(s -> s.getPId() == null).map(item -> {
item.setChildren(getChild(item.getId(), allList));
return item;
}).collect(Collectors.toList());
}
public List<DeviceCategoryDictEntity> getChild(String id, List<DeviceCategoryDictEntity> allList) {
List<DeviceCategoryDictEntity> collect = allList.stream().filter(item -> id.equals(item.getPId())).collect(Collectors.toList());
return collect;
}