*@return*@param<T>*/publicstatic<T> List<T> buildTreeByMap(List<T>list,String idName,String parentIdName,String childrenName,String topParentIdVal) {if(CollectionUtils.isEmpty(list)) {returnCollections.emptyList(); }//根据parentId进行分组Map<String, List<T>> mapList = list.stream().col...
*@return树形结构数据 */publicstaticList<Map<String,Object>>convertToTree(List<Map<String,Object>>list,StringidFieldName,StringparentIdFieldName,StringchildrenFieldName){// 构建节点映射,方便根据id查找节点Map<Object,Map<String,Object>>nodeMap=newHashMap<>();for(Map<String,Object>node:list){Object...
2、转换工具类: public class TreeUtils { //把一个List转成树 static List<Node> buildTree(List<Node> list,Integer pid){ List<Node> tree=new ArrayList<>(); for(Node node:list){ if(Objects.equals(node.getPid(),pid)){ tree.add(findChild(node,list)); } } return tree; } static Node ...
接下来就是具体的工具类了,这个工具类是通用的,因此需要使用泛型(同时使用了递归,就这两个重要的知识点),只要是继承了TreeDot并重写set方法的实体都可以转化: package pri.xiaowd.layui.util;importpri.xiaowd.layui.pojo.TreeDot;importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importj...
定义一个结果Model类 package com.jianshu.vo; import com.jianshu.TreeUtil; import lombok.Data; import lombok.EqualsAndHashCode; /** * @author fubo */ @Data @EqualsAndHashCode(callSuper = true) public class DemoTreeNode extends TreeUtil.TreeNodeDTO { /** * 节点字段1 */ private String colu...
注:该工具类支持将list转换成树/森林。可自行测试,有疑问或更优方案,可私聊我。 TreeNode @Data @JsonInclude(JsonInclude.Include.NON_NULL)publicclassTreeNode<T>{privateString id;privateString key;privateString value; privateString name;privateString parentId;privateString createName;privateLocalDateTime crea...
1 代码 代码语言:javascript 复制 publicclassConvertUtils{publicstaticList<DateSourceType>departmentTransferVO2(List<DateSourceType>list){if(ObjectUtils.isEmpty(list)){returnnull;}Map<String,DateSourceType>map=list.stream().collect(Collectors.toMap(DateSourceType::getId,v->v,(a,b)->a));returngetDepar...
* list 转 tree * @param pId 父级id,list 数据中最顶级的父级id。 * @param list list * @return */publicstaticList<MenuTreeVO>listToTree(Long pId,List<Menu>list){List<MenuTreeVO>ret=newArrayList<>();if(null==list){returnret;}Iterator<Menu>iterable=list.iterator();while(iterable.hasNext...
三、list转树形方法 代码语言:javascript 复制 publicstaticList<NodeVO>streamToTree(List<NodeVO>treeList,Integer parentId){returntreeList.stream()// 过滤父节点.filter(parent->Objects.equals(parent.getPid(),parentId))// 把父节点children递归赋值成为子节点.peek(child->child.setChildren(streamToTree(tr...