为 HashMap 创建自定义迭代器
我正在尝试实现一个迭代器,它将迭代 HashMap 及其重复项。例如
string.put("a", 1);
string.put("a", 2);
string.put("b", 3);
string.put("b", 4);
然而,使用我的迭代器,我只迭代两次,一次针对值为“a”的位置,另一次针对“b”的位置。我想制作一个迭代器,在整个 Map 中迭代 4 次。
编辑:我遗漏了很多细节,因为我只是想看看这个迭代器是否可行,但我正在编写的代码实际上是映射的抽象。例如,我有一个接受参数 T 的 add 函数。因此,对于添加字符串,它看起来像 add("Hello")。通过添加这个字符串,它的键是“String”,它的值是1。如果我再次调用add(“Hello”),它会将值增加到2。如果我再次添加它,它会将它的值增加到3等等。我想创建一个迭代器来迭代我添加的所有内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,Java 的 Map 接口没有不允许重复的键:
因此,当您尝试放入映射中已存在的键时,新值将替换旧值;你的迭代器迭代 2 次的原因是因为只有 2 个项目。
如果你想保留重复的键(比如“a”同时指向 1 和 2),你可以有一个列表或集合的映射,比如
键映射到的列表或集合包含所有值,比如
myMap.get("a")
将返回一个类似于[1,2]
的列表。或者使用 Google 或 Apache
对于您的问题,我相信您是说您想要一个特殊的映射,其中:
您可能想了解一下他们如何为 HashMap,具体看内部私有类
HashIterator
。next()
和hasNext()
可能会按照值指定的次数继续返回键(即,如果"Hello"
映射到2
,您的自定义 iterator() 将返回next()
“Hello”两次,然后再移至下一个键)。地图可能不是适合您想要做的事情的结构,但我祝您好运!
Unfortunately, Java's Map interface doesn't allow for duplicate keys:
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
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:
You may want to look at how they implemented
iterator()
for HashMap, specifically looking at the internal private classHashIterator
.next()
andhasNext()
could maybe keep returning the key for as many times as specified by the value (i.e. if"Hello"
maps to2
, your custom iterator() will return withnext()
"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!
您可以尝试使用
You can try using a Multimap from the guava library.
java.util.HashMap
allows to associate only one value with one key.您可以像这样访问 HashMap 的迭代器:
如果您想循环所有对象,这是一种更快的方法:
您的情况的问题似乎来自于 java 中的 HashMap 不能具有相同的键这一事实,所以你的代码只向 HashMap 添加两个对象。
You can access an Iterator for your HashMap like this :
If you want to loop on all your objects, this is a faster way :
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.