How to get values and keys from HashMap?

JavaHashmap

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

Map is internally made up of Map.Entry objects. Each Entry contains key and value. To get key and value from the entry you use accessor and modifier methods.

If you want to get values with given key, use get() method and to insert value, use put() method.

#Define and initialize map;
Map map = new HashMap();
map.put("USA",1)
map.put("Japan",3)
map.put("China",2)
map.put("India",5)
map.put("Germany",4)

map.get("Germany") // returns 4

If you want to get the set of keys from map, you can use keySet() method

Set keys = map.keySet();
System.out.println("All keys are: " + keys);
// To get all key: value
for(String key: keys){
    System.out.println(key + ": " + map.get(key));
}

Generally, To get all keys and values from the map, you have to follow the sequence in the following order:

  • Convert Hashmap to MapSet to get set of entries in Map with entryset() method.:
    Set st = map.entrySet();
  • Get the iterator of this set:
    Iterator it = st.iterator();
  • Get Map.Entry from the iterator:
    Map.Entry entry = it.next();
  • use getKey() and getValue() methods of the Map.Entry to get keys and values.

// Now access it
Set st = (Set) map.entrySet();
Iterator it = st.iterator();
while(it.hasNext()){
    Map.Entry entry = mapIterator.next();
    System.out.print(entry.getKey() + " : " + entry.getValue());
}

In short, use iterator directly in for

for(Map.Entry entry:map.entrySet()){
    System.out.print(entry.getKey() + " : " + entry.getValue());
}

Solution 3 - Java

You give 1 Dollar, it gives you a cheese burger. You give the String and it gives you the Tab. Use the GET method of HashMap to get what you want.

HashMap.get("String");

Solution 4 - Java

To get values and keys you could just use the methods values() and keySet() of HashMap

public static List getValues(Map map) {
    return new ArrayList(map.values());
}

public static List getKeys(Map map) {
    return new ArrayList(map.keySet());
}

Solution 5 - Java

You could use iterator to do that:

For keys:

for (Iterator <tab> itr= hash.keySet().iterator(); itr.hasNext();) {
    // use itr.next() to get the key value
}

You can use iterator similarly with values.

Solution 6 - Java

for (Map.Entry<String, Tab> entry : hash.entrySet()) {
    String key = entry.getKey();
    Tab tab = entry.getValue();
    // do something with key and/or tab
}

Works like a charm.

Solution 7 - Java

It will work with hash.get("key"); Where key is your key for getting the value from Map

Solution 8 - Java

Use the 'string' key of the hashmap, to access its value which is your tab class.

Tab mytab = hash.get("your_string_key_used_to_insert");

Solution 9 - Java

With java8 streaming API:

List values = map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());

Solution 10 - Java

You have to follow the following sequence of opeartions:

  • Convert Map to MapSet with map.entrySet();
  • Get the iterator with Mapset.iterator();
  • Get Map.Entry with iterator.next();
  • use Entry.getKey() and Entry.getValue()

# define Map
for (Map.Entry entry: map.entrySet)
    System.out.println(entry.getKey() + entry.getValue);

Solution 11 - Java

Using java 8 feature:

    map.forEach((key, value) -> System.out.println(key + " " + value));

Using Map.Entry you can print like this:

 for (Map.Entry<KeyType, ValueType> entry : map.entrySet()) 
 {
     System.out.println(entry.getKey()+" : "+entry.getValue());
 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2279895View Question on Stackoverflow
Solution 1 - JavaBohemianView Answer on Stackoverflow
Solution 2 - JavaPrabhuView Answer on Stackoverflow
Solution 3 - Javauser2245180View Answer on Stackoverflow
Solution 4 - JavaAndriyView Answer on Stackoverflow
Solution 5 - JavabruhhhhhView Answer on Stackoverflow
Solution 6 - JavaGama_aideView Answer on Stackoverflow
Solution 7 - JavaNarendra raghuwanshiView Answer on Stackoverflow
Solution 8 - JavavishakvktView Answer on Stackoverflow
Solution 9 - JavajavabrewView Answer on Stackoverflow
Solution 10 - JavaHary BaktaView Answer on Stackoverflow
Solution 11 - Javaanand krishView Answer on Stackoverflow