您当前的位置: 首页 > 

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【精品】仿京东/淘宝 递归实现电商类别菜单

梁云亮 发布时间:2021-12-10 00:03:36 ,浏览量:2

最终代码可以实现的效果

在这里插入图片描述

类别实体类Category.java
@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;
    }

}
最终查询出来的数据

在这里插入图片描述

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0420s