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 中获取值和键?
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
