java 搜索是否存在指定的键和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17289394/
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
Search if specified key and value exists
提问by navalp3
I am working with hashmap datastructure in java. I have some data in which each entry(value) has a group(key). Now i am storing this data in hashmap as follows
我正在使用 java 中的 hashmap 数据结构。我有一些数据,其中每个条目(值)都有一个组(键)。现在我将这些数据存储在 hashmap 中,如下所示
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "value1");
map.put(1, "value2");
map.put(2, "value3");
map.put(2, "value4");
map.put(3, "value5");
map.put(3, "value6");
map.put(3, "value7");
now I want to search if entry (with key=3 and value="value6") exists in map or not. Is there any specific method to call? or is there and other way to do it?
现在我想搜索条目(key=3 和 value="value6")是否存在于地图中。有什么具体的调用方法吗?或者有没有其他方法可以做到?
回答by Subhrajyoti Majumder
You can not keep multiple entry against same key in a map. If your map previously contained a mapping for the key, the old value is replaced. You need
您不能在地图中针对同一个键保留多个条目。如果您的映射先前包含键的映射,则旧值将被替换。你需要
Map<Integer,List<String>> map = new HashMap<>();
^^^^^
(Diamond operator)
Where you could save List of string against same key. and you can get the value by map#get
您可以在哪里保存针对同一键的字符串列表。你可以通过map#get
List<String> str = map.get(3);
回答by ajduke
You can make use of Guava Multimap(API docs)
您可以使用Guava Multimap(API 文档)
Its stores the multiple values against one key. For your case ,
它针对一个键存储多个值。对于你的情况,
Multimap<Integer,String> myMultimap = ArrayListMultimap.create();
myMultimap .put(1, "value1");
myMultimap .put(1, "value2");
myMultimap .put(2, "value3");
myMultimap .put(2, "value4");
myMultimap .put(3, "value5");
myMultimap .put(3, "value6");
myMultimap .put(3, "value7");
This will create the data structure for you
这将为您创建数据结构
now I want to search if entry (with key=3 and value="value6") exists in map or not. Is there any specific method to call? or is there and other way to do it?
现在我想搜索条目(key=3 和 value="value6")是否存在于地图中。有什么具体的调用方法吗?或者有没有其他方法可以做到?
For searching
use multimap#containsEntry(key,value), which return boolean
result based on the result
对于搜索使用multimap#containsEntry(key,value),它boolean
根据结果返回结果
therefore,
所以,
myMultimap.containsEntry(3,"value6")
which will return true
这将返回 true
回答by Makoto
In broad terms: map.get(key)
will retrieve either the value at this key location, or null
if it doesn't exist.
从广义上讲: map.get(key)
将检索此关键位置的值,或者null
如果它不存在。
Second, you're actually crushing your values. Maps only ever store one value per key. If you want to store multiple values, consider using another collection as the value, which you can add values into later.
其次,你实际上是在破坏你的价值观。Maps 只为每个键存储一个值。如果要存储多个值,请考虑使用另一个集合作为值,稍后您可以向其中添加值。
Here's some sample code:
这是一些示例代码:
//Declaration - change List to Set if duplicates are annoying
Map<Integer, List<String>> map = new HashMap<>();
//Usage - if the list is empty at the key, new one up. Append the value afterwards.
Integer key = Integer.valueOf(1);
List<String> values = map.get(key);
if(key == null) {
values = new ArrayList<>();
}
values.add("word");
map.put(key, values);
Determining the existence of a value at a particular key becomes easy, too:
确定特定键值的存在也变得容易:
public boolean inMap(Map<Integer, List<String>> map, Integer key, String value) {
final List<String> values = map.get(key);
return values != null && values.contains(value);
}
回答by Mikhail Peresypkin
Looks like you need a set of pairs instead of map.
看起来您需要一组对而不是地图。
Set is library class. You can use HashSet for example. Pair is not. You can use http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/tuple/Pair.html
Set 是库类。例如,您可以使用 HashSet。对不是。您可以使用http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/tuple/Pair.html
So,
所以,
// init
Set<Pair<Integer, String>> set = new HashSet<Pair<Integer, String>>();
set.add(new Pair<Integer, String>(1, "1"));
// check
if (set.contains(new Pair<Integer, String>())) {
...
}
回答by Jatin Sehgal
Your key contains only the last value that u put()
for a particular key as the values are overwritten for every key and only the last entered value is stored against the key in the Entry
object.
As per your code your map contains the key value pairs in the following form:
您的键仅包含put()
特定键的最后一个值,因为每个键的值都会被覆盖,并且只有最后输入的值才会存储在Entry
对象中的键上。根据您的代码,您的地图包含以下形式的键值对:
{1=value2, 2=value4, 3=value7}
So, value6
doesnt exist any longer.
所以,已经value6
不存在了。
回答by shreyansh jogi
map stores only unique key and you have stored 3 as key and value6 as value then again 3 as a key and value7 as value then your map conains only 3 as a key and value7 as value value6 will be replaced
地图仅存储唯一键,您已将 3 作为键和 value6 作为值然后再次将 3 作为键和 value7 作为值存储然后您的地图仅包含 3 作为键和 value7 作为值 value6 将被替换
回答by Hola Wald
You can know if a key-pair value exists by getting the value of the desired key and comparing by comparing it to the value.
您可以通过获取所需密钥的值并通过将其与该值进行比较来了解密钥对值是否存在。
Example (Java 7 and above):
示例(Java 7 及更高版本):
boolean exists(Map<K,V> map, K key, V value)
{
return map!=null && map.get(key)!=null && Objects.equals(map.get(key),value);
}
boolean existsinList(Map<K,V> map, K key, V value)
{
return map!=null && map.get(key)!=null && map.get(key).contains(value);
}
All the necessary checks have been included. One may analyze and remove or modify the conditions (eg. map!=null) to fit things as per their use-case and/or convert this function into a single condition that can fit in any control structure (if needed).
所有必要的检查都已包括在内。可以分析并删除或修改条件(例如 map!=null)以根据其用例适应事物和/或将此函数转换为可以适合任何控制结构的单个条件(如果需要)。