Creating a custom iterator for HashMap

I am trying to implement an iterator that will iterate through a HashMap and its duplicates. for instance

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

However, with my iterator, I repeat only twice, once for where the value is "a" and the other for "b". I would like to make an iterator that will be repeated 4 times throughout the map.

EDIT: I forgot a lot of the details a bit, because I just wanted to see if this iterator is possible, but the code I'm writing is actually an abstraction of the map. For example, I have an add function that takes a parameter T. Therefore, to add a line, it will look like add ("Hello"). Adding this string, it has the value "String", and its value is 1. If I call add ("Hello") again, it will increase the value to 2. If I add it again, it will increase its value to 3, and so on. I want to create an iterator that will iterate over all the content that I added.

+4
source share
3 answers

Unfortunately, the Java Map interface does not allow duplicate keys:

An object that maps keys to values. the card cannot contain duplicate keys; each key can display no more than one value.

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

If you want to keep duplicate keys (for example, "a" pointing to 1 and 2), or you can have a list or set map, for example

 Map<String, List<Integer>> myMap; 

If the list or set that matches the key contains all the values, for example myMap.get("a") , it will return a list that will look like [1,2] .

Or use something like MultiMap from Google or Apache


For your problem, I believe you say you want a special card, where:

  • The value for each key is the number of times the key was entered.
  • When you iterate over a card, you iterate over all the keys that you added, every time they were added.

You can see how they implemented iterator() for the HashMap , especially by looking at the internal private class of the HashIterator .

next() and hasNext() could continue to return the key as many times as the value specified (i.e. if "Hello" mapped to 2 , your custom iterator () will return using next() "Hello" two times before go to the next key).

A map may not be the right structure to use for what you are trying to do, but I wish you good luck!

+4
source

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

+3
source

You can access Iterator for your HashMap as follows:

 myHashMap.entrySet.iterator() 

If you want to go in cycles in all your objects, it is faster:

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

The problem in your case seems to be due to the fact that the HashMap in java cannot have the same keys, so your code only adds two objects to the HashMap.

0
source

Source: https://habr.com/ru/post/1342430/


All Articles