Search code examples
javahashmapjtree

Group JTree Nodes Using HashMap


I'm trying to build a JTree using a HashMap, with the Values as the main category and Keys as the sub-category. Essentially it'll look like this:

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

Right now when I try to build the tree, I get a 1-to-1 hierarchy where each key is assigned a new category, even if a category matching 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 end up with any 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));

    }

 }

Solution

  • How can I search through the nodes to make sure I don't end up with any duplicate categories?

    Keep a Map keyed with the Category, and valued with it's Node.

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

    When iterating, check if this Map contains a category Node:

    1. If it does add the Child to that Node
    2. If not create one 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);