1

I've got HashMap with HashSet as value How to iterate over it ti reseive result like:

   key1
   value1
   value2
   value3
   ...
   key2
   value1
   value2
   value3
   key3
   ...

I have something wrong in my code:

Map<String, HashSet<String>> remarksMap = new HashMap<String, HashSet<String>>();
//...

Iterator<Map.Entry<String, HashSet<String>>> itr1 = remarksMap.entrySet().iterator();
Iterator<String> itr2 = itr1.next().getValue().iterator();

    while (itr1.hasNext()) {

        System.out.println(itr1.next().getKey());

        while (itr2.hasNext()) {
            System.out.println(itr2.next());

        }
    }

Currently receiving wrong result like:

key1
value1
value2
value3
key2
key3
key4
...

5 Answers5

2

You don't need explicit Iterators to do this.

Here's a simplified nested iteration with fast-enumeration:

Map<String, HashSet<String>> remarksMap = new HashMap<String, HashSet<String>>();
for (String k: remarksMap.keySet()) {
    System.out.printf("%s%n", k);
    for (String v: remarksMap.get(k)) {
        System.out.printf("\t%s%n", v);
    }
}
Mena
  • 47,782
  • 11
  • 87
  • 106
1

put that Iterator<String> itr2 = itr1.next().getValue().iterator(); into your loop like this:

Map<String, HashSet<String>> remarksMap = new HashMap<String, HashSet<String>>();
//...

Iterator<Map.Entry<String, HashSet<String>>> itr1 = remarksMap.entrySet().iterator();

while (itr1.hasNext()) {

    Map.Entry<String, HashSet<String>> entry = itr1.next();
    System.out.println(entry R.getKey());
    Iterator<String> itr2 = entry .getValue().iterator();

    while (itr2.hasNext()) {
        System.out.println(itr2.next());

    }
}
njank
  • 319
  • 3
  • 12
1

You initialize itr2 with the values of the first element of your map and not with the values of the current element. Adjust it like

Iterator<Map.Entry<String, HashSet<String>>> itr1 = remarksMap.entrySet().iterator();

while (itr1.hasNext()) {
    Map.Entry<String, HashSet<String>> entry = itr1.next();
    System.out.println(entry.getKey());
    Iterator<String> itr2 = entry.getValue().iterator();

    while (itr2.hasNext()) {
        System.out.println(itr2.next());
    }
}
Smutje
  • 17,733
  • 4
  • 24
  • 41
1

Check following code,

    Map<String,HashSet<String>> map = new HashMap<String,HashSet<String>>(); // Map with HashSet as value

    HashSet<String> hashset1 = new HashSet<String>();
    hashset1.add("Midhun");
    hashset1.add("Amal");
    hashset1.add("Ajith");

    HashSet<String> hashset2 = new HashSet<String>();
    hashset2.add("Sooraj");
    hashset2.add("Vinay");
    hashset2.add("Vishnu");

    // Putting data in to Map
    map.put("friends",hashset1);
    map.put("relatives",hashset2);

    for(Entry<String,HashSet<String>> mapEntry : map.entrySet()){
        System.out.println("Key :: "+mapEntry.getKey());
        for(String value : mapEntry.getValue()){
            System.out.println("Value :"+value);
        }
        System.out.println("===================");
    }
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

Move your itr2 assignment to inside your itr1 while loop.

But be careful! Your System.out.println() in the outer loop grabs the next key, and your itr2 assignment currently does the same. Make sure you only call next() once each time you go through the loop, or you'll miss some keys.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67