stream流进行tree树组装
1.准备工具类。
package com.joolun.mall.util; import com.joolun.mall.entity.TreeNode; import lombok.experimental.UtilityClass; import java.util.ArrayList; import java.util.List; /** * @author */ @UtilityClass public class TreeUtil { /** * 两层循环实现建树 * * @param treeNodes 传入的树节点列表 * @return */ public <T extends TreeNode> List<T> build(List<T> treeNodes, Object root) { List<T> trees = new ArrayList<>(); for (T treeNode : treeNodes) { if (root.equals(treeNode.getParentId())) { trees.add(treeNode); // trees.sort(Comparator.comparing(TreeNode::getSort)); } for (T it : treeNodes) { if (it.getParentId().equals(treeNode.getId())) { treeNode.addChildren(it); // treeNode.getChildren().sort(Comparator.comparing(TreeNode::getSort)); } } } return trees; } /** * 使用递归方法建树 * * @param treeNodes * @return */ public <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, Object root) { List<T> trees = new ArrayList<T>(); for (T treeNode : treeNodes) { if (root.equals(treeNode.getParentId())) { trees.add(findChildren(treeNode, treeNodes)); } } return trees; } /** * 递归查找子节点 * * @param treeNodes * @return */ public <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) { for (T it : treeNodes) { if (treeNode.getId() == it.getParentId()) { if (treeNode.getChildren() == null) { treeNode.setChildren(new ArrayList<>()); } treeNode.addChildren(findChildren(it, treeNodes)); } } return treeNode; } }
2.接收实体改造
package com.joolun.mall.entity; import lombok.Data; import java.util.ArrayList; import java.util.List; /** * @author */ @Data public class TreeNode { protected String id; protected String parentId; private Integer sort; protected List<TreeNode> children = new ArrayList<>(); public void addChildren(TreeNode treeNode) { children.add(treeNode); } public List<TreeNode> getChildren() { if(children.size()<=0){ return null; } return children; } }
package com.joolun.mall.entity; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import java.time.LocalDateTime; /** * 商品类目 * * @author www.joolun.com * @date 2019-08-12 11:46:28 */ @Data @EqualsAndHashCode(callSuper = true) public class GoodsCategoryTree extends TreeNode { /** * (1:开启;0:关闭) */ @ApiModelProperty(value = "1:开启;0:关闭") private String enable; /** * 父分类编号 */ @ApiModelProperty(value = "父分类编号") private String parentId; /** * 名称 */ @ApiModelProperty(value = "名称") private String name; /** * 描述 */ @ApiModelProperty(value = "描述") private String description; /** * 图片 */ @ApiModelProperty(value = "图片") private String picUrl; /** * 排序 */ @ApiModelProperty(value = "排序") private Integer sort; }
3.构建树
/** * 构建树 * * @param entitys * @return */ private List<GoodsCategoryTree> getTree(List<GoodsCategory> entitys) {
//重要是下面的stream流的使用 List<GoodsCategoryTree> treeList = entitys.stream() .filter(entity -> !entity.getId().equals(entity.getParentId())) .sorted(Comparator.comparingInt(GoodsCategory::getSort)) .map(entity -> { GoodsCategoryTree node = new GoodsCategoryTree(); BeanUtil.copyProperties(entity,node);//拷陪实体到组装的 tree中 return node; }).collect(Collectors.toList()); return TreeUtil.build(treeList, CommonConstants.PARENT_ID); }
posted on 2022-11-30 20:59 AnkangWenqiang 阅读(305) 评论(0) 编辑 收藏 举报