如何从HashMap获取值和键? [英] How to get values and keys from HashMap?
本文介绍了如何从HashMap获取值和键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Java编写一个简单的编辑文本。当用户打开它时,将在 JTabbedPane
中打开一个文件。我做了以下保存打开的文件:
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:
HashMap< String,Tab> hash = new HashMap< String,Tab>();
其中 Tab
将接收值,例如:文件文件,JTextArea容器,JTabbedPane选项卡
。
Where Tab
will receive the values, such as: File file, JTextArea container, JTabbedPane tab
.
我有一个名为<的类code> Tab :
public Tab(File file, JTextArea container, JTabbedPane tab)
{
this.file = file;
this.container = container;
this.tab = tab;
tab.add(file.getName(), container);
readFile();
}
现在,在此 SaveFile
class,我需要获取存储在 HashMap
中的 Tab
值。我怎么能这样做?
Now, in this SaveFile
class, I need get the Tab
values stored in the HashMap
. How can I do that?
推荐答案
从地图中获取所有值:
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更新:
处理所有值:
Java 8 update:
To process all values:
hash.values().forEach(tab -> /* do something with tab */);
处理所有条目:
hash.forEach((key, tab) -> /* do something with key and tab */);
这篇关于如何从HashMap获取值和键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文