Java 如何从HashMap获取值和键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16246821/
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
How to get values and keys from HashMap?
提问by
I'm writing a simple edit text in Java. When the user opens it, a file will be opened in JTabbedPane
. I did the following to save the files opened:
我正在用 Java 编写一个简单的编辑文本。当用户打开它时,文件将以JTabbedPane
. 我做了以下操作来保存打开的文件:
HashMap<String, Tab> hash = new HashMap<String, Tab>();
HashMap<String, Tab> hash = new HashMap<String, Tab>();
Where Tab
will receive the values, such as: File file, JTextArea container, JTabbedPane tab
.
哪里Tab
会收到值,例如:File file, JTextArea container, JTabbedPane tab
。
I have a class called Tab
:
我有一个班级叫Tab
:
public Tab(File file, JTextArea container, JTabbedPane tab)
{
this.file = file;
this.container = container;
this.tab = tab;
tab.add(file.getName(), container);
readFile();
}
Now, in this SaveFile
class, I need get the Tab
values stored in the HashMap
. How can I do that?
现在,在这个SaveFile
类中,我需要获取Tab
存储在HashMap
. 我怎样才能做到这一点?
采纳答案by Bohemian
To get all the values from a map:
要从地图中获取所有值:
for (Tab tab : hash.values()) {
// do something with tab
}
To get all the entries from a map:
要从地图中获取所有条目:
for ( Map.Entry<String, Tab> entry : hash.entrySet()) {
String key = entry.getKey();
Tab tab = entry.getValue();
// do something with key and/or tab
}
Java 8 update:
Java 8 更新:
To process all values:
处理所有值:
hash.values().forEach(tab -> /* do something with tab */);
To process all entries:
要处理所有条目:
hash.forEach((key, tab) -> /* do something with key and tab */);
回答by vishakvkt
Use the 'string' key of the hashmap, to access its value which is your tab class.
使用哈希映射的“字符串”键,访问它的值,即您的选项卡类。
Tab mytab = hash.get("your_string_key_used_to_insert");
回答by user2245180
You give 1 Dollar, it gives you a cheese burger. You give the String and it gives you the Tab. Use the GET method of HashMap to get what you want.
你给 1 美元,它会给你一个芝士汉堡。你给字符串,它给你标签。使用HashMap的GET方法得到你想要的。
HashMap.get("String");
回答by bruhhhhh
You could use iterator to do that:
您可以使用迭代器来做到这一点:
For keys:
对于钥匙:
for (Iterator <tab> itr= hash.keySet().iterator(); itr.hasNext();) {
// use itr.next() to get the key value
}
You can use iterator similarly with values.
您可以类似地将迭代器与值一起使用。
回答by Prabhu
Map
is internally made up of Map.Entry
objects. Each Entry
contains key
and value
. To get key and value from the entry you use accessor and modifier methods.
Map
内部由Map.Entry
对象组成。每个Entry
包含key
和value
。要从条目中获取键和值,您可以使用访问器和修饰符方法。
If you want to get values
with given key
, use get()
method and to insert value, use put()
method.
如果你想得到给values
定的key
,使用get()
方法并插入值,使用put()
方法。
#Define and initialize map;
Map map = new HashMap();
map.put("USA",1)
map.put("Japan",3)
map.put("China",2)
map.put("India",5)
map.put("Germany",4)
map.get("Germany") // returns 4
If you want to get the set of keys from map, you can use keySet()
method
如果你想从地图中获取一组键,你可以使用keySet()
方法
Set keys = map.keySet();
System.out.println("All keys are: " + keys);
// To get all key: value
for(String key: keys){
System.out.println(key + ": " + map.get(key));
}
Generally, To get all keys and values from the map, you have to follow the sequence in the following order:
通常,要从地图中获取所有键和值,您必须按照以下顺序进行操作:
- Convert
Hashmap
toMapSet
to get set of entries inMap
withentryset()
method.:Set st = map.entrySet();
- Get the iterator of this set:
Iterator it = st.iterator();
- Get
Map.Entry
from the iterator:Map.Entry entry = it.next();
- use
getKey()
andgetValue()
methods of theMap.Entry
to get keys and values.
- 转换
Hashmap
为MapSet
在Map
withentryset()
方法中获取条目集。:Set st = map.entrySet();
- 获取这个集合的迭代器:
Iterator it = st.iterator();
Map.Entry
从迭代器中获取:Map.Entry entry = it.next();
- 的使用
getKey()
和getValue()
方法Map.Entry
来获取键和值。
// Now access it
Set st = (Set) map.entrySet();
Iterator it = st.iterator();
while(it.hasNext()){
Map.Entry entry = mapIterator.next();
System.out.print(entry.getKey() + " : " + entry.getValue());
}
In short, use iterator directly in for
总之,直接在for中使用迭代器
for(Map.Entry entry:map.entrySet()){
System.out.print(entry.getKey() + " : " + entry.getValue());
}
回答by Hary Bakta
You have to follow the following sequence of opeartions:
您必须遵循以下操作顺序:
- Convert
Map
toMapSet
withmap.entrySet();
- Get the iterator with
Mapset.iterator();
- Get
Map.Entry
withiterator.next();
- use
Entry.getKey()
andEntry.getValue()
- 转换
Map
到MapSet
与map.entrySet();
- 获取迭代器
Mapset.iterator();
- 获取
Map.Entry
与iterator.next();
- 使用
Entry.getKey()
和Entry.getValue()
# define Map
for (Map.Entry entry: map.entrySet)
System.out.println(entry.getKey() + entry.getValue);
回答by javabrew
With java8 streaming API:
使用 java8 流 API:
List values = map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
回答by Gama_aide
for (Map.Entry<String, Tab> entry : hash.entrySet()) {
String key = entry.getKey();
Tab tab = entry.getValue();
// do something with key and/or tab
}
Works like a charm.
奇迹般有效。
回答by Narendra raghuwanshi
It will work with hash.get("key");Where key is your key for getting the value from Map
它将与hash.get("key"); 一起使用。其中 key 是您从 Map 获取值的关键
回答by Andriy
To get values and keysyou could just use the methods values() and keySet() of HashMap
要获取值和键,您可以使用 HashMap 的 values() 和 keySet() 方法
public static List getValues(Map map) {
return new ArrayList(map.values());
}
public static List getKeys(Map map) {
return new ArrayList(map.keySet());
}