英文:
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 -> entries.getValue() == a);
基本上,是否有一种方法可以这样说
map.entrySet().removeIf(entries -> 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 -> entries.getValue() == a);
Basically, is there a way to say something like
map.entrySet().removeIf(entries -> entries.getValue() != a);
答案1
得分: 2
你已经拥有它。map.entrySet().removeIf(entries -> entries.getValue() != a)
已经可以工作。(尽管您可能需要使用 !entries.getValue().equals(a)
替代。)
英文:
You already have it. map.entrySet().removeIf(entries -> entries.getValue() != a)
already works. (Though you might have to use !entries.getValue().equals(a)
instead.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论