2 Replies Latest reply on Feb 24, 2010 9:49 PM by 843789

    Iterate over HashMap

    843789
      Hi, I am trying to figure out how to iterate over a HashMap with keys that are Strings and values that are nodes with multiple fields.

      I have information on each node (name, xy position, label, etc) and was wondering how I parse through that information when I want to iterate over specific fields of each node value of the HashMap?
        • 1. Re: Iterate over HashMap
          843789
          Jasmaar wrote:
          Hi, I am trying to figure out how to iterate over a HashMap with keys that are Strings and values that are nodes with multiple fields.
          It is simple to iterate over a HashMap. One way is to get the keySet and iterate over it. For e.g.,
              HashMap<String, Integer> map = new HashMap<String, Integer>();
              map.put("Mon", 1);
              map.put("Tues", 2);
              map.put("Wed", 3);
              map.put("Thurs", 4);
              map.put("Fri", 5);
              
              for (String key : map.keySet()) {
                System.out.println("Key: " + key + "; Value: " + map.get(key));
              }
          Just don't expect any order here.
          I have information on each node (name, xy position, label, etc) and was wondering how I parse through that information when I want to iterate over specific fields of each node value of the HashMap?
          What you do with your values once you obtain them via Map#get(key) is up to you.
          • 2. Re: Iterate over HashMap
            843789
            Maps expose three collections you can iterate over: the keySet, values, and entries. The first two are self-explanatory, and entries is just the key-value pairs in the Map. Choose which one you want to use, and grab that collection from the Map. I've even given you the method names