publicvoidsetChildren(List children){ this.children=children; } //==============TreeNode================ } Service代码使用 List<Template> templateList=templateMapper.selectList(null); List<Template> ret= TreeUtils.generateTrees(templateList);
}publicvoidsetChildren(List<TreeNode>children) {this.children =children; }//定义转换方法1publicstaticList<TreeNode> listGetTree(List<TreeNode>list){ List<TreeNode> treelist =newArrayList<>();for(TreeNode treeNode:list){//找到各个父根,直接插入到treeList中,作为初始节点sif(treeNode.getPid() =...
);List<INodeDTO>sources=entities.stream().map(Factory.NODE_DTO_BUILDER::apply).collect(Collectors.toList());returnINodeDTO.MULTI_TREE_CONVERTER.apply(sources); } } 复制代码 提供一个main方法进行测试。 public static void main(String[]args) throws JsonProcessingException { UseCase useCase =newUse...
System.out.println(JSONUtil.toJsonStr(rootZone1)); // 测试第二种方法 List<Zone> rootZone2 = ZoneUtils.buildTree2(zoneList); System.out.println(JSONUtil.toJsonStr(rootZone2)); // 测试第三种方法 List<Zone> rootZone3 = ZoneUtils.buildTree3(zoneList); System.out.println(JSONUtil.toJson...
List转Tree的两种方法,一种是使用对象的特性,方法极其巧妙,一种是使用Hutool的工具方法,对于复杂的结构可能更友好。 还要递归也能实现,不过网上好多方法,这里不做赘述,如需要递归方法实现请自行百度。 数据准备:一批菜单,结构如下: public class AppMenu { ...
(); //将传进的参数entityList转为MapList List<Map<String, Object>> listMap = JSON.parseObject(JSON.toJSONString(entityList), List.class); //声明一个map用来存listMap中的对象,key为对象id,value为对象本身 Map<String, Map<String, Object>> entityMap = new Hashtable<>(); //循环listMap把...
/**- listToTree- 方法说明- 将JSONArray数组转为树状结构- @param arr 需要转化的数据- @param id 数据唯一的标识键值- @param pid 父id唯一标识键值- @param child 子节点键值- @return JSONArray*/publicstaticJSONArraylistToTree(JSONArrayarr,Stringid,Stringpid,Stringchild){JSONArrayr=newJSONArray()...
staticvoidmain(String[]args)throws JsonProcessingException{List<Node>nodes=Arrays.asList(newNode(1,0,"类别1"),newNode(2,0,"类别2"),newNode(3,0,"类别3"),newNode(4,1,"类别1-1"),newNode(5,2,"类别2-1"),newNode(6,1,"类别1-2"),newNode(7,6,"类别1-2-1"));List<Node>tree...
private class TreeNode{ private String id; private String name; private String parentId; private List<TreeNode> children; // TODO getter/setter } 树构造代码如下: List<TreeNode> menuList = xxManager.findAllMenu(); List<TreeNode> nodeList = new ArrayList<TreeNode>(); ...
使用Gson将List转换为JSONArray Gson库本身没有直接的JSONArray类,但可以将List转换为JsonArray,它类似于JSONArray。 java import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; Gson gson = new Gson(); JsonArray jsonArray = gson.toJsonTree(list).getAs...