hutool树形组件使用

今天做分类的时候需要用到的这个树形组件,顺便记录一下使用的方法,方便以后再次使用

hutool版本

<dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
     <version>5.7.16</version>
</dependency>

实现的方法

public List<Tree<String>> findProductCats() {

        Set<Long> catIds = new HashSet<>();
        for (int i = 0; i < 888; i++) {
            catIds.add((long) RandomUtil.randomInt(1, 666));
        }
        
        // 查询分类信息(数据库查到的数据,这里的id是我自己随机生成的用于测试的)
        List<ProductCategory> catInfos = productCategoryMapper.selectList(new QueryWrapper<ProductCategory>().in("id", catIds));
        
		// 设置参数
        List<TreeNode<String>> collect = catInfos.stream().map(item ->
                new TreeNode<String>().setId(item.getId())
                .setName(item.getName())
                .setParentId(item.getParentId())
                .setWeight(item.getSort())).collect(Collectors.toList());
                
        // 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setChildrenKey("childrenNode");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        return TreeUtil.build(collect, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                });
    }

实体类

@Data
public class ProductCategory {

    private String id;

    private String parentId;

    private String name;

    private Integer level;
    
    private Integer sort;
}

表结构

CREATE TABLE `product_category` (
  `id` varchar(20) NOT NULL COMMENT '分类id',
  `parent_id` varchar(20) DEFAULT NULL COMMENT '上级分类的编号:0表示一级分类',
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '分类名称',
  `level` int(11) DEFAULT NULL COMMENT '分类级别:1->1级;2->2级',
  `sort` int(11) DEFAULT '0' COMMENT '排序',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=744 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='商品分类';
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用Hutool进行树的筛选,您可以按照以下步骤进行操作: 1. 首先,您需要构建一棵树结构并准备好需要筛选的数据。 2. 使用Hutool的树工具类(TreeUtil)来对树进行筛选操作。 3. 调用TreeUtil类的filter方法,传入根节点和一个Predicate对象作为参数。Predicate对象可以自定义,用于指定筛选的条件。 4. 在Predicate对象中实现筛选逻辑,根据节点的属性进行条件判断,返回true或false。 5. filter方法会根据筛选条件遍历整棵树,并返回符合条件的节点列表。 下面是一个示例代码,假设我们有一个Employee类表示员工信息,其中包含id、name和parentId属性: ```java public class Employee { private int id; private String name; private int parentId; // 省略getter和setter方法 } // 创建一些员工数据 Employee root = new Employee(1, "CEO", 0); Employee employee1 = new Employee(2, "A", 1); Employee employee2 = new Employee(3, "B", 1); Employee employee3 = new Employee(4, "C", 2); Employee employee4 = new Employee(5, "D", 3); // 构建树结构 List<Employee> employeeList = Arrays.asList(root, employee1, employee2, employee3, employee4); List<Node<Employee>> nodeList = TreeUtil.build(employeeList, root.getId(), (node, employee) -> employee.getId() == node.getParentId()); // 筛选条件:筛选出parentId为1的节点 List<Node<Employee>> filteredNodes = TreeUtil.filter(nodeList, node -> node.getParentId() == 1); // 输出符合条件的节点 filteredNodes.forEach(node -> System.out.println(node.getId() + " - " + node.getData().getName())); ``` 以上示例代码中,我们使用HutoolTreeUtil类构建了一个树结构,并使用filter方法筛选出parentId为1的节点。 希望对您有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值