Maps

Java HashMap is a hash table based implementation of Java’s Map interface. A Map, as you might know, is a collection of key-value pairs. It maps keys to values.Java HashMap in Collection Hierarchy

Following are few key points to note about HashMaps in Java -

  • A HashMap cannot contain duplicate keys.

  • Java HashMap allows null values and the null key.

  • HashMap is an unordered collection. It does not guarantee any specific order of the elements.

  • Java HashMap is not thread-safe. You must explicitly synchronize concurrent modifications to the HashMap.

Creating a HashMap and Adding key-value pairs to it

The following example shows how to create a HashMap, and add new key-value pairs to it.

import java.util.HashMap;
import java.util.Map;

public class CreateHashMapExample {
    public static void main(String[] args) {
        // Creating a HashMap
        Map<String, Integer> numberMapping = new HashMap<>();

        // Adding key-value pairs to a HashMap
        numberMapping.put("One", 1);
        numberMapping.put("Two", 2);
        numberMapping.put("Three", 3);

        // Add a new key-value pair only if the key does not exist in the HashMap, or is mapped to `null`
        numberMapping.putIfAbsent("Four", 4);

        System.out.println(numberMapping);
    }
}

Accessing keys and modifying their associated value in a HashMap

The example below shows:

  • How to check if a HashMap is empty | isEmpty()

  • How to find the size of a HashMap | size()

  • How to check if a given key exists in a HashMap | containsKey()

  • How to check if a given value exists in a HashMap | containsValue()

  • How to get the value associated with a given key in the HashMap | get()

  • How to modify the value associated with a given key in the HashMap | put()

Removing keys from a HashMap

The following example shows how to :

Obtaining the entrySet, keySet, and values from a HashMap

The Map interface provides methods to retrieve the set of entries (key-value pairs), the set of keys, and the collection of values.

The following example shows how to retrieve them from a HashMap -

Iterating over a HashMap

The following example shows different ways of iterating over a HashMap -

  1. Iterating over a HashMap using Java 8 forEach and lambda expression.

  2. Iterating over the HashMap’s entrySet using iterator().

  3. Iterating over the HashMap’s entrySet using Java 8 forEach and lambda expression.

  4. Iterating over the HashMap’s entrySet using simple for-each loop.

  5. Iterating over the HashMap’s keySet.

Java HashMap with User defined objects

Check out the following example to learn how to create and work with a HashMap of user defined objects.

Problems:

1. Write a Java program to associate the specified value with the specified key in a HashMap. Go to the editor Click me to see the solution

2. Write a Java program to count the number of key-value (size) mappings in a map. Go to the editor Click me to see the solution

3. Write a Java program to copy all of the mappings from the specified map to another map. Go to the editor Click me to see the solution

4. Write a Java program to remove all of the mappings from a map. Go to the editor Click me to see the solution

5. Write a Java program to check whether a map contains key-value mappings (empty) or not. Go to the editor Click me to see the solution

6. Write a Java program to get a shallow copy of a HashMap instance. Go to the editor Click me to see the solution

7. Write a Java program to test if a map contains a mapping for the specified key. Go to the editor Click me to see the solution

8. Write a Java program to test if a map contains a mapping for the specified value. Go to the editor Click me to see the solution

9. Write a Java program to create a set view of the mappings contained in a map. Go to the editor Click me to see the solution

10. Write a Java program to get the value of a specified key in a map. Go to the editor Click me to see the solution

11. Write a Java program to get a set view of the keys contained in this map. Go to the editor Click me to see the solution

12. Write a Java program to get a collection view of the values contained in this map. Go to the editor Click me to see the solution

CC:

Last updated

Was this helpful?