如何获得价值。密钥从HashMap? [英] How to get values. keys from HashMap?

查看:102
本文介绍了如何获得价值。密钥从HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个简单的编辑文本。当用户打开的文件将在 JTabbedPane 中打开时。从中获取文件。我确实保存了打开的文件。



HashMap< String,Tab> hash = new HashMap< String,Tab>();



其中:

选项卡将接收值,例如:文件文件,JTextArea容器,JTabbedPane选项卡



我有一个名为Tab的类:

  public Tab(File file,JTextArea container,JTabbedPane标签)
{
this.file = file;
this.container = container;
this.tab = tab;
tab.add(file.getName(),container);
readFile();




$ b现在,在这个SaveFile类中,我需要获取值存储在HashTab中,以保存文件。如何做到这一点?

解决方案

要从地图获取所有值:

  for(Tab tab:hash.values()){
//用tab
做些事情}



  String key = entry.getKey(); 
Tab tab = entry.getValue();
//用键和/或标签做些事

code $ $ $ $ $ $ $ $更新:

处理所有值:

  hash.values( ).forEach(tab  - > / *用tab * /做些事情); 

处理所有条目:

<$ p $ ((key,tab) - > / *用key和tab做一些事情* /);


I'm writing a simple edit text in Java. When the user open file will be opened in JTabbedPane. To get the file from it. I did to save the files opened.

HashMap<String, Tab> hash = new HashMap<String,Tab>();

Where:

Tab will receive the values, such as: File file, JTextArea container, JTabbedPane 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();

}

Now, in this SaveFile class, I need get the values stored in HashTab, to save the file of the. How I do that ?

解决方案

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:

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 */);

这篇关于如何获得价值。密钥从HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆