Java LinkedHashMap

Posted by Hayri Cicek on September 23, 2019


LinkedHashMap class extends HashMap class and implements the Map interface.
Java LinkedHashMap cannot have duplicate values.
Java LinkedHashMap can have multiple null values and only one null key.
Java LinkedHashMap maintains insertion order.
The following example shows how to create a LinkedHashMap and add key-value pairs to it.


import java.util.LinkedHashMap;

public class LinkedHashMapAddKeyValueExample {
    public static void main(String[] args) {
        LinkedHashMap  cars = new LinkedHashMap<>();
        cars.put(1, "BMW");
        cars.put(2, "Volvo");
        cars.put(3, "Toyota");
        cars.put(4, "Audi");
        cars.put(5, "Mercedes");

        System.out.println("Cars: " + cars);
    }
}


We use put() method to add key-value pairs to a LinkedHashMap.

Output:

$ javac LinkedHashMapAddKeyValueExample.java
$ java LinkedHashMapAddKeyValueExample
Cars: {1=BMW, 2=Volvo, 3=Toyota, 4=Audi, 5=Mercedes}


The following example shows how to use a few LinkedHashMap methods to check the size of the LinkedHashMap, if it is empty, and remove key-value pairs.

import java.util.LinkedHashMap;

public class LinkedHashMapMethodsExample {
    public static void main(String[] args) {
        LinkedHashMap  cars = new LinkedHashMap<>();
        cars.put("BMW", "520");
        cars.put("Volvo", "X90");
        cars.put("Toyota", "Camry");
        cars.put("Audi", "A4");
        cars.put("Mercedes", "S320");
        cars.put("Candy", "mmmmm"); // this is not a car I think

        System.out.println("Cars: " + cars);
        System.out.println("Removing Candy from the map");
        cars.remove("Candy");

        System.out.println("Cars: " + cars);
        System.out.println("Size: " + cars.size());
        System.out.println("containsKey: " + cars.containsKey(1));
        System.out.println("containsValue Audi: " + cars.containsValue("Audi"));
        System.out.println("containsValue Candy: " + cars.containsValue("Candy"));
        System.out.println("isEmpty: " + cars.isEmpty());
        System.out.println("get() method: " + cars.get("Volvo"));
    }
}


remove() method will remove the entry for the specified key if is present.
size() method will return the number of entries in the map.
containsKey() method will return true if the specified key exists in the LinkedHashMap.
containsValue() method will return true if the specified value exists in the LinkedHashMap.
isEmpty() will return true if the LinkedHashMap is empty.
get() method will retrieve the value mapped by the specified key.

Output:

$ javac LinkedHashMapMethodsExample.java
$ java LinkedHashMapMethodsExample
Cars: {BMW=520, Volvo=X90, Toyota=Camry, Audi=A4, Mercedes=S320, Candy=mmmmm}
Removing Candy from the map
Cars: {BMW=520, Volvo=X90, Toyota=Camry, Audi=A4, Mercedes=S320}
Size: 5
containsKey: false
containsValue Audi: false
containsValue Candy: false
isEmpty: false
get() method: X90


The following example shows how to iterate over a LinkedHashMap using Java 8 forEach(),
iterator() method, forEachRemaining() method and the simple for-each loop.

import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;

public class IterateLinkedHashMapExample {
    public static void main(String[] args) {
        LinkedHashMap  cars = new LinkedHashMap<>();
        cars.put("BMW", "520");
        cars.put("Volvo", "X90");
        cars.put("Toyota", "Camry");
        cars.put("Audi", "A4");
        cars.put("Mercedes", "S320");

        System.out.println("Simple for-each loop");
        for (Map.Entry car : cars.entrySet()) {
            System.out.println("I have " + car.getKey() + " " + car.getValue());
        }

        System.out.println("\nJava 8 forEach loop");
        Iterator> carsItr = cars.entrySet().iterator();
        while (carsItr.hasNext()) {
            Map.Entry car = carsItr.next();
            System.out.println("I have " + car.getKey() + " " + car.getValue());
        }

        System.out.println("\nJava 8 forEachRemaining loop");
        carsItr = cars.entrySet().iterator();
        carsItr.forEachRemaining(car -> {
            System.out.println("I have " + car.getKey() + " " + car.getValue());
        });
    }
}


Output:

$ javac IterateLinkedHashMapExample.java
$ java IterateLinkedHashMapExample
Simple for-each loop
I have BMW 520
I have Volvo X90
I have Toyota Camry
I have Audi A4
I have Mercedes S320

Java 8 forEach loop
I have BMW 520
I have Volvo X90
I have Toyota Camry
I have Audi A4
I have Mercedes S320

Java 8 forEachRemaining loop
I have BMW 520
I have Volvo X90
I have Toyota Camry
I have Audi A4
I have Mercedes S320


Share this: