在删除和更新集合时通过哈希图迭代

发布于 2025-02-08 11:43:23 字数 1073 浏览 1 评论 0原文

我有一个计数图,可以在其中跟踪字符串中字符的数量。我想迭代该地图,减少当前访问的角色计数,并在达到零时将其删除。

如何在Java完成?

HashMap<Character, Integer> characterCount = new HashMap<>();
characterCount.put('a', 2);
characterCount.put('b', 1);
characterCount.put('c', 1);

Iterator<Map.Entry<Character, Integer>> iterator = characterCount.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<Character, Integer> entry = iterator.next();

    // Decrement the chosen character from the map
    if (entry.getValue() == 1) {
        iterator.remove();
    } else {
        characterCount.put(entry.getKey(), entry.getValue() - 1);
    }

    // Call some logic the relies on the map with the remaining character count.
    // I want the characterCount.size() to return zero when there is no character with count > 0
    doSomeLogic(characterCount);

    // Restore the character to the map
    characterCount.put(entry.getKey(), entry.getValue());
}

以上代码导致consurrentModificationException

I have a count map where I keep track of the numbers of characters from a string. I want to iterate over that map, decrement the currently visited character count AND remove it if it reaches zero.

How can that be done in Java?

HashMap<Character, Integer> characterCount = new HashMap<>();
characterCount.put('a', 2);
characterCount.put('b', 1);
characterCount.put('c', 1);

Iterator<Map.Entry<Character, Integer>> iterator = characterCount.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<Character, Integer> entry = iterator.next();

    // Decrement the chosen character from the map
    if (entry.getValue() == 1) {
        iterator.remove();
    } else {
        characterCount.put(entry.getKey(), entry.getValue() - 1);
    }

    // Call some logic the relies on the map with the remaining character count.
    // I want the characterCount.size() to return zero when there is no character with count > 0
    doSomeLogic(characterCount);

    // Restore the character to the map
    characterCount.put(entry.getKey(), entry.getValue());
}

The above code results in a ConcurrentModificationException.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

帅的被狗咬 2025-02-15 11:43:23

由于MAP#entrySet返回地图中映射的视图,因此直接设置entry的值以更新它。

if (entry.getValue() == 1) {
    iterator.remove();
} else {
    entry.setValue(entry.getValue() - 1);
}

Since Map#entrySet returns a view of the mappings in the map, directly set the value of the Entry to update it.

if (entry.getValue() == 1) {
    iterator.remove();
} else {
    entry.setValue(entry.getValue() - 1);
}
三生路 2025-02-15 11:43:23

这是一种方法。 map.computeifpresent将在null时删除条目。如果目前是1(即将降低),则使用三元运算符降低值或替换为null

Map<Character, Integer> map = new HashMap<>();
map.put('a', 2);
map.put('b', 1);
map.put('c', 1);

map.computeIfPresent('c', (k,v)-> v == 1 ? null : v-1);
map.computeIfPresent('a', (k,v)-> v == 1 ? null : v-1);

System.out.println(map);

打印

{a=1, b=1}

因此,这是它对您有效的方式,更换迭代器和循环时。

for (char ch : characterCount.keySet()) {
      characterCount.computeIfPresent(ch, (k,v)-> v == 1 ? null : v-1);
}

Here is one way. Map.computeIfPresent will remove the entry when it becomes null. Using the ternary operator either decrements the value or replaces with null if it is presently 1 (about to be decremented).

Map<Character, Integer> map = new HashMap<>();
map.put('a', 2);
map.put('b', 1);
map.put('c', 1);

map.computeIfPresent('c', (k,v)-> v == 1 ? null : v-1);
map.computeIfPresent('a', (k,v)-> v == 1 ? null : v-1);

System.out.println(map);

prints

{a=1, b=1}

So, here is how it would work for you, replacing your iterator and while loop.

for (char ch : characterCount.keySet()) {
      characterCount.computeIfPresent(ch, (k,v)-> v == 1 ? null : v-1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文