JTree group nodes using HashMap

I am trying to create a JTree using a HashMap with the values ​​as the main category being Key and Keys as the sub category. It will essentially look like this:

Movies
   -Marvel
      -The Avengers
      -Guardians of the Galaxy
   -James Bond
      -Casino Royale
      -Skyfall

      

Right now, when I try to build a tree, I end up with a 1 to 1 hierarchy where each key is assigned a new category, even if the category that matches the String already exists. It looks like this:

Movies
   -Marvel
      -The Avengers
   -Marvel
      -Guardians of the Galaxy

      

Below is my code. How can I search through the nodes to make sure I don't have duplicate categories?

public class JTree extends JFrame {
        private javax.swing.JTree genreTree;
        private JPanel panel1;

public JTree() throws IOException{
    super("Genre Search");
    setContentPane(panel1);
    pack();
    setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    setVisible(true);
    setSize(new Dimension(500,500));

    genreTree.setModel(null);

    HashMap<String, String> hash = new HashMap<String, String>();
    hash.put("Avengers","Marvel");
    hash.put("Guardians","Marvel");
    hash.put("Casino Royale","James Bond");
    hash.put("Skyfall","James Bond");

    String genreName = "Movies";
    DefaultMutableTreeNode genreMainTree = new
            DefaultMutableTreeNode(genreName);
    DefaultMutableTreeNode mediaTitleNode = new DefaultMutableTreeNode("");
    DefaultMutableTreeNode universeTitleNode = new
            DefaultMutableTreeNode("");


    Set<String> keys = hash.keySet();
    Collection<String> values = hash.values();

    ArrayList<Map.Entry<String,String>> copy = new
            ArrayList<Map.Entry<String, String>>();
    copy.addAll(hash.entrySet());

    for (Map.Entry<String,String> e : copy){

        mediaTitleNode = new DefaultMutableTreeNode(e.getKey());

        universeTitleNode = new DefaultMutableTreeNode(e.getValue());

        genreMainTree.add(universeTitleNode);

        universeTitleNode.add(mediaTitleNode);

    }

    genreTree.setModel(new DefaultTreeModel(genreMainTree));

    }

 }

      

+3


source to share


1 answer


How can I search through the nodes to make sure I don't have any duplicate categories?

Save the map associated with the category and rate it with Node.

Map<String, DefaultMutableTreeNode> categoryToNode = new HashMap<>();

      

On retry, check if this map contains the Node category:



  • If it adds Child to this Node
  • If you do not create it and add it to the Tree and Map.

For example:

DefaultMutableTreeNode universeTitleNode = categoryToNode.get(e.getValue());
if (universeTitleNode == null ){
    universeTitleNode = new DefaultMutableTreeNode(e.getValue());
    categoryToNode.put(e.getValue(), universeTitleNode);
    genreMainTree.add(universeTitleNode);
}
mediaTitleNode = new DefaultMutableTreeNode(e.getKey());
universeTitleNode.add(mediaTitleNode);

      

+3


source







All Articles