Constructors and methods of LinkedHashMap in Java

There are five constructors defined for LinkedHashMap class.

  • LinkedHashMap()
  • LinkedHashMap(int initialCapacity)
  • LinkedHashMap(int initialCapacity, float loadFactor)
  • LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
  • LinkedHashMap(Map<? extends K,? extends V> m)

 

LinkedHashMap() – This is default constructor which is used to create new and empty LinkedHashMap with initial capacity 16 and the load factor .75. Inside this constructor, we initialize the load factor with its default value i.e 0.75f  Let’s see the internal code of this constructor.

public LinkedHashMap() {
        super();
        accessOrder = false;
    }

Example –

package linkedhashmapiter;
 
import java.util.*;
 
 
public class Example1 {
	public static void main(String[] args) {
	
		// creating LinkedHashMap object using default constructor
		Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>();
		linkedHashMapObj.put(2, "ram");
		linkedHashMapObj.put(8, "mohan");
		linkedHashMapObj.put(3, "sohan");
		linkedHashMapObj.put(4, "rahul");
		linkedHashMapObj.put(9, "rohan");
		linkedHashMapObj.put(0, "suresh");
		linkedHashMapObj.put(1, "ganesh");
 
		
		Iterator<Map.Entry<Integer, String>> iterator = linkedHashMapObj
				.entrySet().iterator();
	 
		while (iterator.hasNext()) {
			Map.Entry<Integer, String> entry = iterator.next();
			System.out.println("Key is = " + entry.getKey() + ", Value is = "
					+ entry.getValue());
		}
	}
}

Output is –

Key is = 2, Value is = ram
Key is = 8, Value is = mohan
Key is = 3, Value is = sohan
Key is = 4, Value is = rahul
Key is = 9, Value is = rohan
Key is = 0, Value is = suresh
Key is = 1, Value is = ganesh

LinkedHashMap(int initialCapacity) –  We can create a new LinkedHashMap defining some different capacity than 16 using this constructor(default factor still will be 0.75f).

Internal implementation –

public LinkedHashMap(int initialCapacity) {
        super(initialCapacity);
        accessOrder = false;
    }

Example –

package linkedhashmapconstructor;

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

public class Example2 {
public static void main(String[] args) {
	
			//creating LinkedHashMap object using initial capacity 20 instead of default capacity 16
			Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>(20);
			linkedHashMapObj.put(2, "ram");
			linkedHashMapObj.put(8, "mohan");
			linkedHashMapObj.put(3, "sohan");
			linkedHashMapObj.put(4, "rahul");
			linkedHashMapObj.put(9, "rohan");
			linkedHashMapObj.put(0, "suresh");
			linkedHashMapObj.put(1, "ganesh");
	 
			
			Iterator<Map.Entry<Integer, String>> iterator = linkedHashMapObj
					.entrySet().iterator();
		 
			while (iterator.hasNext()) {
				Map.Entry<Integer, String> entry = iterator.next();
				System.out.println("Key is = " + entry.getKey() + ", Value is = "
						+ entry.getValue());
			}
}
}

Output is –

Key is = 2, Value is = ram
Key is = 8, Value is = mohan
Key is = 3, Value is = sohan
Key is = 4, Value is = rahul
Key is = 9, Value is = rohan
Key is = 0, Value is = suresh
Key is = 1, Value is = ganesh



 

LinkedHashMap(int initialCapacity, float loadFactor) – We can also create a new LinkedHashMapwith new capacity as well as new load factor, rather than 16 and 0.75.

Internal implementation –

 public LinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        accessOrder = false;
    }

Example –

package hashtableconstructor;

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

public class Example3 {
public static void main(String[] args) {
	
	//creating LinkedHashMap object using initial capacity 20 and loadfactor 0.80f
	Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>(20,0.80f);
	linkedHashMapObj.put(2, "ram");
	linkedHashMapObj.put(8, "mohan");
	linkedHashMapObj.put(3, "sohan");
	linkedHashMapObj.put(4, "rahul");
	linkedHashMapObj.put(9, "rohan");
	linkedHashMapObj.put(0, "suresh");
	linkedHashMapObj.put(1, "ganesh");

	
	Iterator<Map.Entry<Integer, String>> iterator = linkedHashMapObj
			.entrySet().iterator();
 
	while (iterator.hasNext()) {
		Map.Entry<Integer, String> entry = iterator.next();
		System.out.println("Key is = " + entry.getKey() + ", Value is = "
				+ entry.getValue());
	}
}
}

Output is –

Key is = 2, Value is = ram
Key is = 8, Value is = mohan
Key is = 3, Value is = sohan
Key is = 4, Value is = rahul
Key is = 9, Value is = rohan
Key is = 0, Value is = suresh
Key is = 1, Value is = ganesh

LinkedHashMap(Map map) – We can pass any type of classes which implements Map interface and can convert it into LinkedHashMap. For example, we are going to convert HashMap into LinkedHashMap in below example before that let’s see how this constructor has been defined in LinkedHashMap class.

    public LinkedHashMap(Map<? extends K, ? extends V> m) {
        super();
        accessOrder = false;
        putMapEntries(m, false);
    }

Example –

package linkedhashmapconstructor;

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

public class Example4 {
public static void main(String[] args) {
	
			//creating HashMap object using, later we will convert into LinkedHashMap
			Map<Integer, String> hashmapObject = new HashMap<>();
			hashmapObject.put(2, "ram");
			hashmapObject.put(8, "mohan");
			hashmapObject.put(3, "sohan");
			hashmapObject.put(4, "rahul");
			hashmapObject.put(9, "rohan");
			hashmapObject.put(0, "suresh");
			hashmapObject.put(1, "ganesh");

			
			// we will convert Hashtable into HashMap using below constructor
			LinkedHashMap<Integer,String> linkedHashMapObj = new LinkedHashMap<>(hashmapObject);
			Iterator<Map.Entry<Integer, String>> iterator = linkedHashMapObj
					.entrySet().iterator();
		 
			while (iterator.hasNext()) {
				Map.Entry<Integer, String> entry = iterator.next();
				System.out.println("Key is = " + entry.getKey() + ", Value is = "
						+ entry.getValue());
			}
}
}

Output is –

Key is = 0, Value is = suresh
Key is = 1, Value is = ganesh
Key is = 2, Value is = ram
Key is = 3, Value is = sohan
Key is = 4, Value is = rahul
Key is = 8, Value is = mohan
Key is = 9, Value is = rohan

 

For method details visit documenation.