user3942128
user3942128

Reputation:

Itereate HashMap with HashSet as values

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
...

Upvotes: 1

Views: 6440

Answers (5)

Rakesh KR
Rakesh KR

Reputation: 6527

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("===================");
    }

Upvotes: 1

Mena
Mena

Reputation: 48404

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

Upvotes: 2

chiastic-security
chiastic-security

Reputation: 20520

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.

Upvotes: 0

Smutje
Smutje

Reputation: 18143

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

Upvotes: 1

njank
njank

Reputation: 328

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

    }
}

Upvotes: 1

Related Questions