Java 在不迭代的情况下从地图中删除条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19438785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove entry from map without iterating
提问by monal86
How do I remove entry from JavaMap
without using iteration using value or key. Basically in my map, I am using containsKey()
then map.remove()
to remove it.
如何在不使用值或键的迭代的情况下从Java 中删除条目Map
。基本上在我的地图中,我使用containsKey()
thenmap.remove()
将其删除。
回答by nanofarad
public V remove(Object key)
Removes the mapping for this key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that
(key==null ? k==null : key.equals(k))
, that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key. (A null return can also indicate that the map previously associated null with the specified key if the implementation supports null values.) The map will not contain a mapping for the specified key once the call returns.
public V remove(Object key)
如果存在,则从此映射中删除此键的映射(可选操作)。更正式地说,如果此映射包含从键 k 到值 v 的
(key==null ? k==null : key.equals(k))
映射,则该映射将被删除。(地图最多可以包含一个这样的映射。)返回映射先前与键关联的值,如果映射不包含此键的映射,则返回 null。(如果实现支持 null 值,则返回 null 也可以指示映射先前将 null 与指定的键关联。)一旦调用返回,映射将不包含指定键的映射。
Basically you can call remove, even if the key does not exist. It will silently fail in that case(returning null). The object need not be identical or the same, if yourKey.equals(key)
is true for the key you want to remove.
基本上你可以调用remove,即使键不存在。在这种情况下它会默默地失败(返回 null)。对象不必相同或相同,如果yourKey.equals(key)
您要删除的键为真。
回答by rgettman
Use Map
's remove
method, which takes a key as an argument. You don't need the original object; you just need an object that compares equal to the existing key in the map with equals
, and produces the same hashCode
as the existing key in the map.
UseMap
的remove
方法,它以一个键作为参数。你不需要原始对象;您只需要一个对象,该对象与映射中的现有键进行比较equals
,并生成与映射中的现有键相同的对象hashCode
。
回答by Trying
public Object remove(Object key)
remove
method in Map
.
remove
中的方法Map
。
From javadoc:
从javadoc:
Removes the mapping for this key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)),
that mapping is removed. (The map can contain at most one such mapping.)
如果存在,则从此映射中删除此键的映射(可选操作)。更正式地说,如果此映射包含从键 k 到值 v 的(key==null ? k==null : key.equals(k)),
映射,则该映射将被删除。(地图最多可以包含一个这样的映射。)
Returns the value to which the map previously associated the key, or null
if the map contained no mapping for this key. (A null
return can also indicate that the map previously associated null
with the specified key if the implementation supports null
values.) The map will not contain a mapping for the specified key once the call returns.
返回映射先前与键关联的值,或者null
如果映射不包含此键的映射。(如果实现支持值null
,返回也可以指示先前null
与指定键关联的映射null
。)一旦调用返回,映射将不包含指定键的映射。
Parameters:
参数:
key - key whose mapping is to be removed from the map.
key - 要从映射中删除其映射的键。
Returns:
回报:
Previous value associated with specified key, or null
if there was no mapping for key.
与指定键关联的先前值,或者null
如果没有键的映射。
Example:
例子:
Map<Integer, String> map=new HashMap<>();
map.put(20, "stackOverflow");
System.out.println(map.remove(20));
This code will print "stackOverflow"
i.e. Previous value associated with specified key. Hope it helps.
此代码将打印"stackOverflow"
即与指定键关联的先前值。希望能帮助到你。
回答by Alfredo Osorio
You remove an entry from a map by using the key of the element that you wish to remove.
您可以使用要删除的元素的键从地图中删除条目。
map.remove("aKey");
If you don't know the key of the element you must iterate to obtain it such as this:
如果您不知道元素的键,则必须迭代以获取它,例如:
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
Set<T> keys = new HashSet<T>();
for (Entry<T, E> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
keys.add(entry.getKey());
}
}
return keys;
}
This will return all the keys that were found on the map.
这将返回在地图上找到的所有键。
回答by mo sean
Map<Integer, String> abcMap = new HashMap<Integer, String>();
abcMap.put(1,"A");
abcMap.put(2,"B");
abcMap.put(3,"C");
/// now for remove item
abcMap.remove(1);
// this will remove "A" from abcMap..
回答by Will Hains
It seems nobody here actually answered OP's question. They were asking how, without using an iterator, to remove an entry "using value or key". Of course remove(key)
works if you have the key, but not if you are searching for a value.
似乎这里没有人真正回答 OP 的问题。他们询问如何在不使用迭代器的情况下删除“使用值或键”的条目。remove(key)
如果您有密钥,当然可以使用,但如果您正在搜索value则不行。
Alfredo's answerwas closest, in that it searches the value side of the Map
, but it uses an iterator.
Alfredo 的答案最接近,因为它搜索 的值侧Map
,但它使用迭代器。
Here's a solution using Java 8's Stream
API; no iterators:
这是使用 Java 8 的Stream
API的解决方案;没有迭代器:
Map<K, V> newMap = map.entrySet().stream()
.filter(entry -> !entry.getKey().equals(searchValue))
.filter(entry -> !entry.getValue().equals(searchValue))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
(Substitute K
and V
for your key and value types.)
(替换K
和V
用于您的键和值类型。)
回答by Simax
In most cases you can remove entry (key-value pair) from a map using Map.remove(key)
method. There will be no iteration over the map. You don't need to use Map.containsKey(key)
before the Map.remove(key)
because Map.remove
already does it. It returns either a previous associated value with key
or null
if there was no mapping for the key
.
在大多数情况下,您可以使用Map.remove(key)
方法从地图中删除条目(键值对)。将不会在地图上进行迭代。你不需要在Map.containsKey(key)
之前使用,Map.remove(key)
因为Map.remove
已经这样做了。它返回无论是先前的相关值和key
或null
是否有对没有映射关系key
。
But you can't do the same thing using a value only (without key). The only option is to iterate via map and find entry (or entries) with that value.
但是你不能只使用一个值(没有键)来做同样的事情。唯一的选择是通过 map 进行迭代并找到具有该值的条目(或多个条目)。
Java 5-7:
爪哇 5-7:
for (Iterator<MyValue> it = map.values.iterator(); it.hasNext(); ) {
if(it.next().equals(myValue)) {
it.remove();
}
}
Java 8:
爪哇 8:
map.values().removeIf(v -> v.equals(myValue));
回答by Gank
public static void main(String[] args) {
//Constant.mapCustomer.remove("s");
Map<String, String> mapCustomer=new HashMap<String, String> ();;
mapCustomer.put("s","ss");
System.out.println(mapCustomer.get("s"));
mapCustomer.remove("s");
System.out.println(mapCustomer.get("s"));
}
public static void main(String[] args) {
//Constant.mapCustomer.remove("s");
Map<String, String> mapCustomer=new HashMap<String, String> ();;
mapCustomer.put("s","ss");
System.out.println(mapCustomer.get("s"));
mapCustomer.remove("s");
System.out.println(mapCustomer.get("s"));
}