为 HashMap 创建自定义迭代器

发布于 2024-10-20 14:29:21 字数 442 浏览 1 评论 0原文

我正在尝试实现一个迭代器,它将迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-10-27 14:29:21

不幸的是,Java 的 Map 接口没有不允许重复的键:

将键映射到值的对象。一个
映射不能包含重复的键;
每个键最多可以映射到一个值。

因此,当您尝试放入映射中已存在的键时,新值将替换旧值;你的迭代器迭代 2 次的原因是因为只有 2 个项目。

如果你想保留重复的键(比如“a”同时指向 1 和 2),你可以有一个列表或集合的映射,比如

Map<String, List<Integer>> myMap;

键映射到的列表或集合包含所有值,比如 myMap.get("a") 将返回一个类似于 [1,2] 的列表。

或者使用 GoogleApache


对于您的问题,我相信您是说您想要一个特殊的映射,其中:

  1. 每个键的值是输入该键的次数。
  2. 当您迭代地图时,每次添加时都会迭代您添加的所有键。

您可能想了解一下他们如何为 HashMap,具体看内部私有类HashIterator

next()hasNext() 可能会按照值指定的次数继续返回键(即,如果 "Hello" 映射到2,您的自定义 iterator() 将返回 next()“Hello”两次,然后再移至下一个键)。

地图可能不是适合您想要做的事情的结构,但我祝您好运!

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!

别在捏我脸啦 2024-10-27 14:29:21

您可以尝试使用

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

那请放手 2024-10-27 14:29:21

您可以像这样访问 HashMap 的迭代器:

myHashMap.entrySet.iterator()

如果您想循环所有对象,这是一种更快的方法:

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

您的情况的问题似乎来自于 java 中的 HashMap 不能具有相同的键这一事实,所以你的代码只向 HashMap 添加两个对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文