This example shows how to add elements to LinkedHashMap in Java. This example also shows how to add mappings (key-value pairs) to LinkedHashMap using put, putAll, and putIfAbsent methods.
How to add key-value mappings to LinkedHashMap in Java?
There are several ways using which you can add new key value mappings to a LinkedHashMap object as given below.
1. Add new key-value mapping using the put method
Use the put
method of the LinkedHashMap class to add a key-value mapping to the LinkedHashMap object.
1 |
public V put(K key, V value) |
The put
method associates the given key with the specified value in the LinkedHashMap object if the mapping does not exist for the key and returns null.
If the specified key is already mapped to any value in the map, the existing value is replaced with the specified new value and old value is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.LinkedHashMap; public class LinkedHashMapAddNewMappingsExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); /* * To create a new key-value mapping to a * LinkedHashMap object, use the put method */ /* * this will add 1=>One mapping to the LinkedHashMap * and returns null */ System.out.println( lhmap.put(1, "One") ); //this will add 2=>Two and returns null System.out.println( lhmap.put(2, "Two") ); /* * This will replace "One" with "Eleven" * for the key 1 and returns "One" */ System.out.println( lhmap.put(1, "Eleven") ); System.out.println("LinkedHashMap contains: " + lhmap); } } |
Output
1 2 3 4 |
null null One LinkedHashMap contains: {1=Eleven, 2=Two} |
Important Note: The LinkedHashMap class allows null values to be associated with the key. Do not rely on the null return value of the put
method to determine whether the key existed or not in the map.
A null value can also be returned by the put
method if the given key was associated with a null value in the map.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); //this will return null as the key 1 does not exist System.out.println( lhmap.put(1, null) ); /* * this will also return null as the key 1 exists, * but it is associated with the null value currently */ System.out.println( lhmap.put(1, "Eleven") ); System.out.println("LinkedHashMap contains: " + lhmap); |
Output
1 2 3 |
null null LinkedHashMap contains: {1=Eleven} |
2. Using the putIfAbsent method
As we have seen in the above example, the put
method replaces an old value with the new value if the key exists in the map. If you do not want to replace the value if the key exists. then use the putIfAbsent
method.
1 |
public V putIfAbsent(K key, V value) |
The putIfAbsent
method adds a new key-value mapping to the LinkedHashMap object if the key does not exist in the map and returns null. If the key exists in the map, it simply returns the value associated with the given key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); /* * This will add 1=>One to the LinkedHashMap and * returns null since key 1 does not exist */ System.out.println( lhmap.putIfAbsent(1, "One") ); /* * This will return "One", the value * associated with the key 1 (no replacement) */ System.out.println( lhmap.putIfAbsent(1, "Eleven") ); System.out.println("LinkedHashMap contains: " + lhmap); |
Output
1 2 3 |
null One LinkedHashMap contains: {1=One} |
Difference between put and putIfAbsent methods:
If the key does not exist in the LinkedHashMap object, both the put
and putIfAbsent
methods behave the same. Both of them add a key-value mapping to the map object.
But if the key exists, the put
method replaces an old value with the new value but the putIfAbsent
method does not.
3. Using the putAll method
The putAll
method of the LinkedHashMap class copies all the mappings from the specified map to this LinkedHashMap object.
1 |
public void putAll(Map<? extends K,? extends V> m) |
The putAll
method also replaces all the values of a LinkedHashMap object with the values contained in the specified map for the mappings having the same key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
HashMap<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(1, "One"); hmap.put(2, "Two"); hmap.put(3, "Three"); LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(3, "Thirty"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); /* * The putAll method adds all the mappings of the * specified map to this linked hash map object * * This will add new mappings for keys 1 and 2 * and replace value "Thirty" with "Three" for * the key 3 */ lhmap.putAll(hmap); System.out.println("LinkedHashMap contains: " + lhmap); |
Output
1 |
LinkedHashMap contains: {3=Three, 4=Four, 5=Five, 1=One, 2=Two} |
You can also add all the mappings from the TreeMap using the putAll
method as it accepts any Map object.
This example is a part of the Java LinkedHashMap tutorial with examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap