Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));

    }

 }
like image 887
Michelle Avatar asked Apr 20 '26 13:04

Michelle


1 Answers

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);
like image 107
copeg Avatar answered Apr 22 '26 04:04

copeg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!