Thursday, 8 August 2013

How to iterate through a HashMap with two pointers?

How to iterate through a HashMap with two pointers?

I want to search a HashMap for duplicates. Currently this is my HashMap:
HashMap<String, HashMap<String, String>>
I was going to create two Iterators, one i and another j, and two loops.
The first while loop will have the index of i and then the second loop
will have the index of j but j is == to i before the loop begans.
Iterator<Entry<String, HashMap<String, String>>> i =
listings.entrySet().iterator();
while(i.hasNext()) {
HashMap<String, String> entry = i.next().getValue();
Iterator<Entry<String, HashMap<String, String>>> j = i;
while(j.hasNext()) {
j.next();
// DO STUFF
}
}
But this doesn't work because when i call j.next(), it also changes the
index of i.

No comments:

Post a Comment