Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashMap key value storage and retrieval

Tags:

I want to store values and retrieve them from a Java HashMap.

This is what I have so far:

public void processHashMap()
{
    HashMap hm = new HashMap();
    hm.put(1,"godric gryfindor");
    hm.put(2,"helga hufflepuff"); 
    hm.put(3,"rowena ravenclaw");
    hm.put(4,"salazaar slytherin");
}

I want to retrieve all Keys and Values from the HashMap as a Java Collection or utility set (for example LinkedList).

I know I can get the value if I know the key, like this:

hm.get(1);

Is there a way to retrieve key values as a list?

like image 860
Arjun K P Avatar asked Jun 15 '12 10:06

Arjun K P


People also ask

How key and values are stored in HashMap?

HashMaps use an inner class to store data: the Entry<K, V>. This entry is a simple key-value pair with two extra data: a reference to another Entry so that a HashMap can store entries like singly linked lists. a hash value that represents the hash value of the key.

How values are stored in HashMap?

HashMap stores elements in so-called buckets and the number of buckets is called capacity. When we put a value in the map, the key's hashCode() method is used to determine the bucket in which the value will be stored. To retrieve the value, HashMap calculates the bucket in the same way – using hashCode().

What is HashMap can we store objects in HashMap and how do you retrieve them?

HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.

Can we store object in HashMap key?

Yes, we can use any object as key in a Map in java but we need to override the equals() and hashCode() methods of that object class. Please refer an example below, in which I am storing an object of Pair class as key in a hashMap with value type as string in map.


Video Answer


1 Answers

I use these three ways to iterate a map. All methods (keySet, values, entrySet) return a collection.

// Given the following map
Map<KeyClass, ValueClass> myMap;

// Iterate all keys
for (KeyClass key  : myMap.keySet()) 
    System.out.println(key);

// Iterate all values
for (ValueClass value  : myMap.values()) 
    System.out.println(value);

// Iterate all key/value pairs
for (Entry<KeyClass, ValueClass> entry  : myMap.entrySet()) 
    System.out.println(entry.getKey() + " - " + entry.getValue());

Since Java 8 i often use Streams with lambda expressions.

    // Iterate all keys
    myMap.keySet().stream().forEach(key -> System.out.println(key));

    // Iterate all values
    myMap.values().parallelStream().forEach(value -> System.out.println(value));

    // Iterate all key/value pairs
    myMap.entrySet().stream().forEach(entry -> System.out.println(entry.getKey() + " - " + entry.getValue()));
like image 185
Joost Avatar answered Oct 20 '22 09:10

Joost