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
Tab
将接收值的地方,例如:文件文件、JTextArea 容器、JTabbedPane 选项卡
.
Where Tab
will receive the values, such as: File file, JTextArea container, JTabbedPane tab
.
我有一个名为 Tab
的类:
I have a class called 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
类中,我需要获取存储在 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 更新:
处理所有值:
hash.values().forEach(tab -> /* do something with tab */);
处理所有条目:
hash.forEach((key, tab) -> /* do something with key and tab */);
这篇关于如何从 HashMap 中获取值和键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 HashMap 中获取值和键?


基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java Swing计时器未清除 2022-01-01