Java-根据父级id将List结构转Tree结构
List的stream
public ResultData queryMenuList() {
// 获取所有数据List List<MenuVo> list = MenuDao.queryMenuList(); // 通过list.stream()将List结构转成Tree结构并返回 List<MenuVo> collection = list.stream() .filter(o -> StrUtil.equals("-1", o.getPIdString())) .peek(o -> o.setChildList(getChildList(o, list))) .collect(Collectors.toList()); return ResultData.success(Constants.QUERY_SUCCESS, collection, collection.size()); } // 递归方法 private List<MenuVo> getChildList(MenuVo MenuVo, List<MenuVo> list) { return list.stream() .filter(o -> StrUtil.equals(String.valueOf(MenuVo.getIdString()), o.getPIdString())) .peek(o -> o.setChildList(getChildList(o, list))) .collect(Collectors.toList());