java list 转树 tree 的三种写法

List对象数组转为tree

代码是自己写的,今天学习来的。下一步,写成工具类。

pom里面加上:阿里的fastjson
 
  


  net.sf.json-lib
  json-lib
  2.4
 
  
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 

* * @package: com.tree * @class: TreeNode1 * @date: 2018/3/27 * @author: jcroad(caoyajing @ yunmel.com) * @since 1.0 */ public class TreeNode { private Integer id; private Integer pid; private String name; private List children; TreeNode(Integer id, Integer pid, String name) { this.id = id; this.pid = pid; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } public static void main(String[] args) { List list = new ArrayList(); list.add(new TreeNode(1, 0, "1")); list.add(new TreeNode(2, 0, "2")); list.add(new TreeNode(3, 2, "3")); list.add(new TreeNode(4, 3, "4")); list.add(new TreeNode(5, 4, "5")); list.add(new TreeNode(6, 5, "6")); List treeList = new ArrayList(); List treeList1 = new ArrayList(); List treeList2 = new ArrayList(); List treeList3 = new ArrayList(); //方法一、 treeList = listGetStree(list); treeList1 = listToTree(list); treeList2 = toTree(list); System.out.println(JSON.toJSONString(treeList)); System.out.println(JSON.toJSONString(treeList1)); System.out.println(JSON.toJSONString(treeList2)); } private static List listGetStree(List list) { List treeList = new ArrayList(); for (TreeNode tree : list) { //找到根 if (tree.getPid() == 0) { treeList.add(tree); } //找到子 for (TreeNode treeNode : list) { if (treeNode.getPid() == tree.getId()) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList()); } tree.getChildren().add(treeNode); } } } return treeList; } /** * 方法二、 * @param list * @return */ public static List listToTree(List list) { //用递归找子。 List treeList = new ArrayList(); for (TreeNode tree : list) { if (tree.getPid() == 0) { treeList.add(findChildren(tree, list)); } } return treeList; } private static TreeNode findChildren(TreeNode tree, List list) { for (TreeNode node : list) { if (node.getPid() == tree.getId()) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList()); } tree.getChildren().add(findChildren(node, list)); } } return tree; } /** *方法三 * @param list * @return */ private static List toTree(List list) { List treeList = new ArrayList(); for (TreeNode tree : list) { if(tree.getPid() == 0){ treeList.add(tree); } } for (TreeNode tree : list) { toTreeChildren(treeList,tree); } return treeList; } private static void toTreeChildren(List treeList, TreeNode tree) { for (TreeNode node : treeList) { if(tree.getPid() == node.getId()){ if(node.getChildren() == null){ node.setChildren(new ArrayList()); } node.getChildren().add(tree); } if(node.getChildren() != null){ toTreeChildren(node.getChildren(),tree); } } } }

你可能感兴趣的:(java基础)