Java Programming Techniques and APIs

A Quick Guide to Linked Hashmap in Java

A Quick Guide to Linked Hashmap in Java

Java Class: Linked Hashmap

The Java LinkedHashMap class implements the Map interface by combining Hashtables and Linked Lists. It has predictable iteration order. This class inherits the HashMap class and implements the Map interface.

Few things to remember :

  • LinkedHashMaps contain values based on their keys.
  • A LinkedHashMap in Java contains unique elements.
  • The Java LinkedHashMap can store a null key and a null value.
  • The Java LinkedHashMap does not support synchronization.
  • LinkedHashMaps in Java maintains insertion order.
  • Java's HashMap class has a default capacity of 16, with a 0.75 load factor.

Syntax

It is necessary to import the Java.util.LinkedHashMap package before creating a linked hashmap.

// Here, we are creating a linked hash map with an initial capacity of 8 and a load factor of 0.6
LinkedHashMap<Key, Value> numbers = new LinkedHashMap<>(8, 0.6f);

Here,

Key - A unique identifier for each element of a map (value)

Value - a map's elements that correspond to its keys

Java LinkedHashMap class Constructors

Constructor

Description

LinkedHashMap()

This function constructs a default LinkedHashMap.

LinkedHashMap(int capacity)

This function creates a LinkedHashMap with the given capacity.

LinkedHashMap(int capacity, float loadFactor)

This function initializes both the capacity and the load factor.

LinkedHashMap(int capacity, float loadFactor, boolean accessOrder)

Using a specified ordering mode, it initializes both capacity and load factor.

LinkedHashMap(Map<? extends K,? extends V> m)

This method initializes the LinkedHashMap with the elements of the given Map class.

LinkedHashMap Methods

The following are some of the methods provided by LinkedHashMap class for performing various tasks on the map.

Element Insertion into LinkedHashMap.

  • put() -

This function inserts the key/value mapping into the map.

  • putAll() -

This map contains all the entries from the specified map.

  • putIfAbsent() -

In the absence of the specified key, inserts the specified key/value mapping into the map.

Accessing Elements from LinkedHashMap

1. Using entrySet(), keySet() and values()

  • entrySet() -

This function returns a set of the map's key/value mappings.

  • keySet() -

Returns a list of all the map's keys

  • values() -

Provides a list of all map values

2. Using get() and getOrDefault()

  • get() -

Specifies the key and returns the value corresponding to that key. If no key exists, it returns null.

  • getOrDefault() -

The key value returns if the key exists. It returns the default value if the key does not exist.

Remove Elements from LinkedHashMap

  • remove(key) -

This function deletes the entry associated with the specified key from the map.

  • remove(key, value) -

Using this function, you remove a key from a map based on whether it maps to a value and return a boolean.

Code Sample

The following code will demonstrate how to use linked hashmap in java along with performing various operations.

import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        // Creating LinkedHashMap of even numbers
        LinkedHashMap<String, Integer> oddNumbers = new LinkedHashMap<>();

        // Using put()
        oddNumbers.put("Three", 3);
        oddNumbers.put("Five", 5);
        System.out.println("Original LinkedHashMap: " + oddNumbers);

        // Using putIfAbsent()
        oddNumbers.putIfAbsent("one", 1);

        oddNumbers.putIfAbsent("Seven", 7);
        System.out.println("Updated LinkedHashMap(): " + oddNumbers);

        //Creating LinkedHashMap of numbers
        LinkedHashMap<String, Integer> numbers = new LinkedHashMap<>();
        numbers.put("Nine", 9);

        // Using putAll()
        numbers.putAll(oddNumbers);
        System.out.println("New LinkedHashMap: " + numbers);
       
       
        // Using entrySet()
        System.out.println("Key/Value mappings: " + numbers.entrySet());

        // Using keySet()
        System.out.println("Keys: " + numbers.keySet());

        // Using values()
        System.out.println("Values: " + numbers.values());
       
        // remove method with single parameter
        int value = numbers.remove("one");
        System.out.println("Removed value: " + value);

        // remove method with two parameters
        boolean result = numbers.remove("Three", 3);
        System.out.println("Is entry with Key = Three removed? " + result);

        System.out.println("Updated LinkedHashMap: " + numbers);

    }
}


Output

Original LinkedHashMap: {Three=3, Five=5}

Updated LinkedHashMap(): {Three=3, Five=5, one=1, Seven=7}

New LinkedHashMap: {Nine=9, Three=3, Five=5, one=1, Seven=7}

Key/Value mappings: [Nine=9, Three=3, Five=5, one=1, Seven=7]

Keys: [Nine, Three, Five, one, Seven]

Values: [9, 3, 5, 1, 7]

Removed value: 1Is entry with Key = Three removed? true

Updated LinkedHashMap: {Nine=9, Five=5, Seven=7}

write your code here: Coding Playground