递归
List<ZlCategory> list = zlCategoryService.findAll(); List<ZlCategory> rootList = new ArrayList<>(); list.forEach(c -> { if (c.getParentId().equals(0L)) rootList.add(c); }); rootList.forEach(c -> c.setChildList(getChild(c.getId(), list)));
private List<ZlCategory> getChild(Long id, List<ZlCategory> list) { List<ZlCategory> childList = new ArrayList<>(); list.forEach(c -> { if (c.getParentId().equals(id)) childList.add(c); }); //递归 childList.forEach(c -> c.setChildList(getChild(c.getId(), list))); if (childList.size() == 0) return new ArrayList<>(); return childList; }