Java 如何从 Map 获取特定的键和值

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

How to get particular key and value from Map

javacollections

提问by Hitesh Kumar

I have searched but could not find a way to print only single key, value pair for particular entry.

我已经搜索过,但找不到只打印特定条目的单个键值对的方法。

map.containsValue(value) or map.containsKey(key)

map.containsValue(value) or map.containsKey(key)

will only tell if particular value or key is available or not. I want to print that particular key, value pair if (value is available)In other link, They want to get the random value, if you do not know the key. Here I know key and value and want to print it for particular key and value.

只会告诉特定值或键是否可用。我想打印那个特定的键,值对if (value is available)在其他链接中,如果您不知道键,他们想要获得随机值。在这里,我知道键和值,并希望为特定的键和值打印它。

采纳答案by sauumum

There is no avalilable method in https://docs.oracle.com/javase/7/docs/api/java/util/Map.htmlto get Entity from map if there is any key or value available. Try below code if it help :

如果有任何可用的键或值,https://docs.oracle.com/javase/7/docs/api/java/util/Map.html 中没有可用的方法从地图中获取实体。如果有帮助,请尝试以下代码:

if (map.containsKey(key)) {
   Object value = map.get(key);
 System.out.println("Key : " + key +" value :"+ value);
 }

回答by Jitendra Nagar

Access pair using Key
DataType value =myMap.get(s);
Operation on value if u want (optional)
myMap.put(s,value)
and for accessing whole map use Iterator of Map.Entry set

使用 Key
DataType 值的访问对 =myMap.get(s);
如果你想要(可选)
myMap.put(s,value)
并且为了访问整个地图使用 Map.Entry 集的迭代器,则对值进行操作