TreeBuilder { public List<TM> buidTree() { return this.buidTree(parentNotFoundAction); } TreeBuilder(Collection<TM> datas); <T> TreeBuilder(Collection<T> datas, TreeModelCreator<TM, T> treeNodeCreator); <T> TreeBuilder(Collection<T> datas, TreeModelCreator<TM, T> treeNodeCreator, Comparator<TM> comparator); TreeBuilder<TM> rootIds(Object...objects); TreeBuilder<TM> rootNodeFunc(RootNodeFunc<TM> rootNodeFunc); TreeBuilder<TM> ignoreParentNotFound(); TreeBuilder<TM> parentNotFound(ParentNodeNotFoundAction<TM> parentNotFoundAction); List<TM> buidTree(); List<TM> buidTree(ParentNodeNotFoundAction<TM> notFoundAction); List<TM> reverseRoots(); TreeModel<?> getBranch(Object rootId); List<TM> getRootNodes(); Map<Object, TM> getNodeMap(); static void main(String[] args);  }
@Test public void testBuildTree() { TreeNodeDataTest data1 = new TreeNodeDataTest(1L, null, "node1"); TreeNodeDataTest data2 = new TreeNodeDataTest(2L, 1L, "node2"); TreeNodeDataTest data3 = new TreeNodeDataTest(3L, 2L, "node3"); TreeNodeDataTest data4 = new TreeNodeDataTest(4L, 1L, "node4"); TreeNodeDataTest data5 = new TreeNodeDataTest(5L, 4L, "node5"); List<TreeNodeDataTest> datas = Lists.newArrayList(data1, data2, data3, data4, data5); List<DefaultTreeModel> models = datas.stream().map(d->{ return new DefaultTreeModel(d.getId(), d.getName(), d.getParentId()); }).collect(Collectors.toList()); TreeBuilder<DefaultTreeModel> treeBuilder = new TreeBuilder<>(models); List<DefaultTreeModel> roots = treeBuilder.buidTree(); System.out.println("roots: \n" + roots.get(0)); StringBuilder str = new StringBuilder(); TreeUtils.buildString(str, roots.get(0), "--"); System.out.println("roots: \n" + str); }