Java 根据键从哈希图中删除值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18429684/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:33:42  来源:igfitidea点击:

Remove a value from hashmap based on key

javahashmap

提问by user2704112

So, I have a HashMap<String, String>

所以,我有一个 HashMap<String, String>

public HashMap<String, String> frozen = new HashMap<String, String>();

and I want to remove a value from it depending on the key. So lets say I put in these

我想根据键从中删除一个值。所以让我们说我把这些

frozen.put("1", "1_1");
frozen.put("1", "1_2");

I want to remove only one of the values, not the whole keyset.

我只想删除其中一个值,而不是整个键集。

How would I go about doing this? If you still didn't understand, this non-existing method should explain it.

我该怎么做呢?如果你还是不明白,这个不存在的方法应该解释一下。

frozen.remove("1", "1_2");

Obviously that doesn't exist, but that's what I want.

显然这不存在,但这就是我想要的。

Thanks in advance.

提前致谢。

采纳答案by Josh M

It seems the easiest solution would be to use some type of Listas your value. In this particular case, it might look something like this:

似乎最简单的解决方案是使用某种类型List作为您的价值。在这种特殊情况下,它可能看起来像这样:

final Map<String, List<String>> map = new HashMap<String, List<String>>();
final String key = "1";
map.put(key, new LinkedList<String>());
map.get(key).add("1_1");
map.get(key).add("1_2");

And then to remove a value given a particular key (as shown in your question), you may try something like map.get(key).remove("1_2");

然后要删除给定特定键的值(如您的问题所示),您可以尝试类似 map.get(key).remove("1_2");

回答by Erik Pragt

What you are doing is not possible. You cannot put in a different value using the same key. You can remove an entry using the remove method, but for something you want, you can checkout something like Guava's MultiMap.

你在做什么是不可能的。您不能使用相同的键输入不同的值。您可以使用remove 方法删除条目,但对于您想要的内容,您可以查看Guava 的 MultiMap 之类的内容

If you really want to remove based on a value, you'll need to iterate through the values of the Map using entrySet(), and then based on the value call the remove method using the found key.

如果你真的想根据一个值删除,你需要使用 entrySet() 遍历 Map 的值,然后根据该值使用找到的键调用 remove 方法。

回答by RaptorDotCpp

The easiest way to find a solution to such a problem is to consult the JavaDocs: http://docs.oracle.com/javase/7/docs/api/

找到此类问题的解决方案的最简单方法是查阅 JavaDocs:http: //docs.oracle.com/javase/7/docs/api/

Find your class there (in your case: java.util.HashMap) and look for the remove method. In this case, you do not need to hand the value to the method (that would be really inefficient). To remove something from a HashMap, simply hand the key to the remove method:

在那里找到您的类(在您的情况下:java.util.HashMap)并查找 remove 方法。在这种情况下,您不需要将值传递给方法(这将非常低效)。要从 HashMap 中删除某些内容,只需将键传递给 remove 方法:

frozen.remove("1");

This will remove the key-value pair "1", "1_2"from the HashMap.

这将从"1", "1_2"HashMap 中删除键值对。

Why will this not remove the first key-value pair? Because the putmethod overwrites any previous values. To be able to map a key to multiple values, try creating a HashMap with a String and a List:

为什么这不会删除第一个键值对?因为该put方法会覆盖任何以前的值。为了能够将一个键映射到多个值,请尝试使用一个字符串和一个列表创建一个 HashMap:

HashMap<String, ArrayList> frozen = new HashMap<String, ArrayList>();

is a possible example.

是一个可能的例子。

回答by Veronica Cornejo

You probably have put's parameter order inverted. Duplicate keys are not allowed. New values (for the same key) replace the older. So,

您可能将put的参数顺序颠倒了。不允许使用重复的密钥。新值(对于同一个键)替换旧值。所以,

frozen.put("1", "1_1");
frozen.put("1", "1_2");

produces a map with only one entry: key="1", and value="1_2". On the contrary,

生成只有一个条目的映射:key="1" 和 value="1_2"。相反,

frozen.put("1_1", "1" );
frozen.put("1_2", "1" );

produces a map with 2 entries. To remove an entry, you only need to reference its key, as they are unique:

生成一个包含 2 个条目的地图。要删除条目,您只需要引用其键,因为它们是唯一的:

frozen.remove("1_2");

If this doesn't ring a bell, then please be more specific in what the data structure should hold, and what not. A few use cases would help.

如果这没有敲响警钟,那么请更具体地说明数据结构应该包含什么,不应该包含什么。一些用例会有所帮助。

回答by Ravi Thapliyal

I believe you're trying to map multiple strings to one key. It's possible but if you map your key to a List.

我相信您正在尝试将多个字符串映射到一个键。这是可能的,但如果您将密钥映射到List.

public Map<String, List<String>> frozen = new HashMap<String, List<String>>();

Then you can add multiple values to the same key as

然后您可以将多个值添加到相同的键

public void addToMappedList(Map<String, List<String>> map,
                                 String key, String value) {
    List<String> existingValues = map.get(key);
    if (existingValues == null) {
        existingValues = new ArrayList<String>();
        map.put(key, existingValues);
    }
    existingValues.add(value);
}

addToMappedList(frozen, "1", "1_1");
addToMappedList(frozen, "1", "1_2");

Here's how to go about removing individual values from the List. The booleanreturned would indicate if the valuewas actually found and removed from the Listor not.

以下是如何从List. 返回的布尔值将指示是否value实际找到并从 中删除List

public boolean removeFromMappedList(Map<String, List<String>> map,
                                         String key, String value) {
    List<String> existingValues = map.get(key);
    if (existingValues != null) {
        return existingValues.remove(value);
    }
    return false;
}

removeFromMappedList(frozen, "1", "1_1"); // true
removeFromMappedList(frozen, "1", "1_3"); // false

To remove the whole key and the Listassociated with it just use the Mapdirectly

要删除整个密钥及其List关联,只需Map直接使用

frozen.remove("1");