012-list 转树 tree
一、概述
二、代码
public class ListTreeConvert { private Integer id; private Integer pid; private String name; private List<ListTreeConvert> children; ListTreeConvert(Integer id, Integer pid, String name) { this.id = id; this.pid = pid; this.name = name; } public ListTreeConvert() { } 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<ListTreeConvert> getChildren() { return children; } public void setChildren(List<ListTreeConvert> children) { this.children = children; } public static void main(String[] args) { List<ListTreeConvert> list = new ArrayList<ListTreeConvert>(); list.add(new ListTreeConvert(1, 0, "1")); list.add(new ListTreeConvert(2, 0, "2")); list.add(new ListTreeConvert(3, 2, "3")); list.add(new ListTreeConvert(4, 3, "4")); list.add(new ListTreeConvert(5, 4, "5")); list.add(new ListTreeConvert(6, 5, "6")); List<ListTreeConvert> treeList1 = new ArrayList<>(); List<ListTreeConvert> treeList2 = new ArrayList<>(); List<ListTreeConvert> treeList3 = new ArrayList<>(); String src =JSON.toJSONString(list); //方法一、 treeList1 = listToTree1(JSONArray.parseArray(src,ListTreeConvert.class)); treeList2 = listToTree2(JSONArray.parseArray(src,ListTreeConvert.class)); treeList3 = listToTree3(JSONArray.parseArray(src,ListTreeConvert.class)); System.out.println(JSON.toJSONString(treeList1)); System.out.println(JSON.toJSONString(treeList2)); System.out.println(JSON.toJSONString(treeList3)); } public static List<ListTreeConvert> listToTree1(List<ListTreeConvert> list) { List<ListTreeConvert> treeList = new ArrayList<ListTreeConvert>(); for (ListTreeConvert tree : list) { //找到根 if (tree.getPid() == 0) { treeList.add(tree); } //找到子 for (ListTreeConvert treeNode : list) { if (treeNode.getPid() == tree.getId()) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList<ListTreeConvert>()); } tree.getChildren().add(treeNode); } } } return treeList; } /** * 方法二、 * * @param list * @return */ public static List<ListTreeConvert> listToTree2(List<ListTreeConvert> list) { //用递归找子。 List<ListTreeConvert> treeList = new ArrayList<ListTreeConvert>(); for (ListTreeConvert tree : list) { if (tree.getPid() == 0) { treeList.add(findChildren(tree, list)); } } return treeList; } private static ListTreeConvert findChildren(ListTreeConvert tree, List<ListTreeConvert> list) { for (ListTreeConvert node : list) { if (node.getPid() == tree.getId()) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList<ListTreeConvert>()); } tree.getChildren().add(findChildren(node, list)); } } return tree; } /** * 方法三 * * @param list * @return */ public static List<ListTreeConvert> listToTree3(List<ListTreeConvert> list) { List<ListTreeConvert> treeList = new ArrayList<ListTreeConvert>(); for (ListTreeConvert tree : list) { if (tree.getPid() == 0) { treeList.add(tree); } } for (ListTreeConvert tree : list) { toTreeChildren(treeList, tree); } return treeList; } private static void toTreeChildren(List<ListTreeConvert> treeList, ListTreeConvert tree) { for (ListTreeConvert node : treeList) { if (tree.getPid() == node.getId()) { if (node.getChildren() == null) { node.setChildren(new ArrayList<ListTreeConvert>()); } node.getChildren().add(tree); } if (node.getChildren() != null) { toTreeChildren(node.getChildren(), tree); } } } }