如何删除java hashmap中特定对象的所有映射?

如何删除java hashmap中特定对象的所有映射?,java,hashmap,Java,Hashmap,我在java hashmap中有一个多对一映射。我使用java.util.HashMap.values()迭代所有值。现在,如果我想删除一个特定的值和所有对应的键,那么我应该怎么做 仅使用java.util.HashMap.remove(对象键)和其中一个键就足够了吗?在本例中,您可以从映射中删除值而无需迭代 代码 // The test map final Map<String, String> map = new HashMap<String, String>();

我在java hashmap中有一个多对一映射。我使用
java.util.HashMap.values()
迭代所有值。现在,如果我想删除一个特定的值和所有对应的键,那么我应该怎么做


仅使用
java.util.HashMap.remove(对象键)
和其中一个键就足够了吗?

在本例中,您可以从映射中删除值而无需迭代

代码

// The test map
final Map<String, String> map = new HashMap<String, String>();
map.put("Key1", "Value");
map.put("Key2", "Value");
map.put("Key3", "Value");

// Remove the map. The collection is necessary to remove all values instead of just one.
map.values().removeAll(Collections.singleton("Value"));

// Print the map to confirm it worked.
System.out.println("Printing Map");
for(final String key : map.keySet()) {
   System.out.println(key + " = " + map.get(key));
}

在本例中,您可以从贴图中删除值,而无需迭代

代码

// The test map
final Map<String, String> map = new HashMap<String, String>();
map.put("Key1", "Value");
map.put("Key2", "Value");
map.put("Key3", "Value");

// Remove the map. The collection is necessary to remove all values instead of just one.
map.values().removeAll(Collections.singleton("Value"));

// Print the map to confirm it worked.
System.out.println("Printing Map");
for(final String key : map.keySet()) {
   System.out.println(key + " = " + map.get(key));
}

是的,删除所有绑定在键上的值就足够了。是的,删除所有绑定在键上的值就足够了。可能的副本即将发布类似的内容。我有两个值,x和y。删除行是
map.values().removeIf(y::equals)。同样的结果。如果您使用的是Java8+并且即将发布非常类似的内容,那么这将非常有用。我有两个值,x和y。删除行是
map.values().removeIf(y::equals)。同样的结果。如果您使用的是Java8,那么这很有用+