Here you can find the source of getTree(List
Parameter | Description |
---|---|
paths | the paths |
public static TreeNode getTree(List<TreePath> paths)
//package com.java2s; //License from project: Open Source License import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Main { /**//w w w . ja v a2 s . c o m * constract tree from list of paths. * * @param paths the paths * @return the tree */ public static TreeNode getTree(List<TreePath> paths) { return getTree(paths, null); } /** * constract tree from list of paths. * * @param paths the paths * @param root the root * @return the tree */ public static TreeNode getTree(List<TreePath> paths, String root) { // check for empty list if (paths == null || paths.isEmpty()) return null; //new DefaultMutableTreeNode("EMPTY"); // iterate over paths Map<String, DefaultMutableTreeNode> map = new LinkedHashMap<String, DefaultMutableTreeNode>(); for (TreePath path : paths) { DefaultMutableTreeNode parent = null; for (Object n : path.getPath()) { DefaultMutableTreeNode node = map.get("" + n); if (node == null) { node = new DefaultMutableTreeNode("" + n); map.put("" + n, node); } // add as child to parent if (parent != null) parent.add(node); parent = node; } } // the root should be the very first entry in linked table return (map.containsKey(root)) ? map.get(root) : map.get(map .keySet().iterator().next()); } }