从哈希映射中移除没有特定值的项目

huangapple go评论63阅读模式
英文:

Removing items from a Hashmap that DOESN'T have a specific value

问题

我有一个 Hashmap<ArrayList<Integer>, Integer>。键是包含 b 和 c 的整数列表,符合以下方程式

> a = n*b+c

而值为 a。我只想要那些给我特定值 a(例如 3)的键 (a, b)。我知道如何使用值来删除键值对,但我需要帮助做相反的操作。我想保留所有具有相同值的键值对

a.map.entrySet().removeIf(entries -&gt; entries.getValue() == a);

基本上,是否有一种方法可以这样说

map.entrySet().removeIf(entries -&gt; entries.getValue() != a);
英文:

I have a Hashmap<ArrayList<Integer>, Integer>. The keys are an integer list containing b and c in this equation

> a = n*b+c

and the value is a. I only want the keys (a,b) that give me a specific value of a, for example 3. I am aware of how to remove key/value pairs using a value, but I need help doing the opposite. I want to keep all the key/value pairs that has the same value

a.map.entrySet().removeIf(entries -&gt; entries.getValue() == a);

Basically, is there a way to say something like

map.entrySet().removeIf(entries -&gt; entries.getValue() != a);

答案1

得分: 2

你已经拥有它。map.entrySet().removeIf(entries -&gt; entries.getValue() != a) 已经可以工作。(尽管您可能需要使用 !entries.getValue().equals(a) 替代。)

英文:

You already have it. map.entrySet().removeIf(entries -&gt; entries.getValue() != a) already works. (Though you might have to use !entries.getValue().equals(a) instead.)

huangapple
  • 本文由 发表于 2020年10月16日 03:27:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定