最终代码可以实现的效果
@Getter
@Setter
@Builder
@ToString
class Category {
private Integer id;
private String name;
private Integer pid;
private Integer sort;
private List children;
}
类别模拟数据
public class DB {
/**
* 初始化数据
*
* @return
*/
public static List init() {
Random random = new Random();
List categoryList = new ArrayList();
categoryList.add(new Category(1, "aa", null, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(2, "bb", null, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(3, "aa1", 1, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(4, "aa2", 1, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(5, "bb1", 2, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(6, "bb2", 2, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(7, "aa11", 3, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(8, "aa12", 3, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(9, "aa13", 4, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(10, "aa14", 4, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(11, "bb15", 5, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(12, "bb16", 5, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(13, "bb17", 6, random.nextInt(20), new ArrayList()));
categoryList.add(new Category(14, "bb18", 6, random.nextInt(20), new ArrayList()));
return categoryList;
}
}
实现一
public class Category3Demo1 {
@Test
public void fun() {
List categoryList = DB.init();
//保存最终的结果
List res = new ArrayList();
for (Category category : categoryList) {
//一级类别
if (category.getPid() == null) {
res.add(category);
}
}
//遍历所有的一级类别,求它的子类别
for (Category item : res) {
item.setChildren(getChildren(item, categoryList));
}
Collections.sort(res, Comparator.comparingInt(Category::getSort));
System.out.println(JsonUtil.obj2String(res));
}
/**
* 递归:求参数category在list中所有的子类别
* @param category
* @param list
* @return
*/
private List getChildren(Category category, List list) {
//子类别
List childList = new ArrayList();
//遍历list求出category的子类别
for (Category item : list) {
if (category.getId().equals(item.getPid())) {
childList.add(item);
}
}
for (Category item : childList) {
item.setChildren(getChildren(item, list));
}
Collections.sort(childList, Comparator.comparingInt(Category::getSort));
return childList;
}
}
实现二
public class Category3Demo2 {
@Test
public void fun() {
List categoryList = DB.init();
//找到一级分类
List res = categoryList.stream()
.filter(item -> item.getPid() == null)
.map(item -> {
item.setChildren(getChildren(item, categoryList));
return item;
})
.sorted((c1, c2) -> c1.getSort() - c2.getSort())
.collect(Collectors.toList());
System.out.println(JsonUtil.obj2String(res));
}
private List getChildren(Category category, List all) {
List childList = all.stream()
.filter(item -> category.getId().equals(item.getPid()))
.map(item -> {
item.setChildren(getChildren(item, all));
return item;
})
.sorted(Comparator.comparingInt(Category::getSort))
.collect(Collectors.toList());
return childList;
}
}
实现三
public class Category3Demo3 {
@Test
public void fun() {
List categoryList = DB.init();
//找到一级分类
List res = categoryList.stream()
.filter(item -> item.getPid() == null)
.peek(item -> item.setChildren(getChildren(item, categoryList)))
.sorted((c1, c2) -> c1.getSort() - c2.getSort())
.collect(Collectors.toList());
System.out.println(JsonUtil.obj2String(res));
}
private List getChildren(Category category, List all) {
List childList = all.stream()
.filter(item -> category.getId().equals(item.getPid()))
.peek(item -> item.setChildren(getChildren(item, all)))
.sorted(Comparator.comparingInt(Category::getSort))
.collect(Collectors.toList());
return childList;
}
}
最终查询出来的数据