4

I am trying to implement an iterator that will iterate through the HashMap and its duplicates. For example

string.put("a", 1);
string.put("a", 2);
string.put("b", 3);
string.put("b", 4);

However with my iterator I only iterate twice, once for where the value is "a" and the other for "b". I'd like to make an iterator that will iterate 4 times throughout the whole Map.

EDIT: I kind of left out a lot of detail because I just wanted to see if this iterator was possible, but the code I'm writing is actually an abstraction of a map. For example, I have an add function which takes in parameter T. So for adding a string it would look like add("Hello"). By adding this string, it's key is "String" and its value is 1. If I call add("Hello") again it will bump up the value to 2. If I add it again, it will bump up its value to 3 and so on. I want to create an iterator that will iterate through all the stuff I have added.

Isai
  • 41
  • 2
  • 5

3 Answers3

4

Unfortunately, Java's Map interface doesn't allow for duplicate keys:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Thus, when you try to put a key that already exists in the map, the new value replaces the old value; the reason your iterator iterates 2 times is because there are only 2 items.

If you want to keep duplicate keys (like "a" pointing to both 1 and 2), either you can have a Map of a List or Set, like

Map<String, List<Integer>> myMap;

Where the list or set that a key maps to contains all values, like myMap.get("a") would return a list that would look like [1,2].

Or use something like a MultiMap from either Google or Apache


For your problem, I believe you are saying that you want a special Map where:

  1. The value for each key to be how many times the key was entered.
  2. When you iterate over the map, you iterate over all the keys you added, for each time they were added.

You may want to look at how they implemented iterator() for HashMap, specifically looking at the internal private class HashIterator.

next() and hasNext() could maybe keep returning the key for as many times as specified by the value (i.e. if "Hello" maps to 2, your custom iterator() will return with next() "Hello" two times before moving onto the next key).

Map may not be an appropriate structure to use for what you're trying to do, but I wish you luck!

Zach L
  • 16,072
  • 4
  • 38
  • 39
  • Yup you got it, thats what I am trying to iterate. I got it to work where I would copy over the map to an arraylist and put in the keys that however many times its value is. However, this is not very efficient. – Isai Mar 06 '11 at 21:41
3

You can try using a Multimap from the guava library. java.util.HashMap allows to associate only one value with one key.

Alex Nikolaenkov
  • 2,505
  • 20
  • 27
0

You can access an Iterator for your HashMap like this :

myHashMap.entrySet.iterator()

If you want to loop on all your objects, this is a faster way :

for(Object o : myHashMap.entrySet()) {
     // do something with o
}

The problem in your case seems to come from the fact that a HashMap in java can't have identical keys, so your code add only two objects to the HashMap.

krtek
  • 26,334
  • 5
  • 56
  • 84