How to get values and keys from HashMap?
hashmap, java
Solution
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 */);
Problem
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>();` 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 `Tab` values stored in the `HashMap`. How can I do that?