×

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are:
  1. It contains the values based on the keys.
  2. It maintains the order of insertion.
  3. It contains unique elements.
  4. It contains one null key and multiple null values.
  5. It is not synchronized.
  6. The initial capacity is 16, and the load factor is 0.75f.

Declaration of LinkedHashMap

LinkedHashMap<String, String> lm= new LinkedHashMap<String, String> ();
Constructors in LinkedHashMap:
  1. LinkedHashMap (): This is used to create default LinkedHashMap constructor.
  2. LinkedHashMap (int capacity): It is used to set the LinkedHashMap with specified capacity.
  3. LinkedHashMap (int capacity, float Loadfactor): It is used to set the LinkedHashMap with capacity and Load factor.
  4. LinkedHashMap (Map m): This is used to set the LinkedHashMap with elements from the given Map.
  5. LinkedHashMap (int capacity, float Load factor, boolean order): This is used to set the LinkedHashMap with capacity and Loadfactor along with specified order.

Difference between LinkedHashMap and LinkedHashSet:

LinkedHashMap

LinkedHashSet

1. LinkedHashMap implements Map interface and inherits HashMap class. 1. LinkedHashSet implements Set interface and inherits HashSet class.
2. It replaces the value with duplicate keys. 2. It does not change the original value.
3. It was mapping the keys to values. 3. It simply stores the values.
4. It enables one null key and multiple null values. 4. It permits only one null value.
5. It consists of five types of constructors. 5. It consists of four types of constructors.
6.Declaration:  LinkedHashMap<Integer, String> lm= new LinkedHashMap<Integer, String> (); 6.Declaration: LinkedHashSet<String> lh= new LinkedHashSet<String> ();

Methods of LinkedHashMap:

  1. clear (): It is used to clear all the mapping from the map.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
l.clear ();
System.out.println ("Finally the maps after clear: " + l);
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
Finally the maps after clear: {}
  1. containsKey (Object K): It returns true if the specified key is present on the map.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Is the key '20' present?" + l.containsKey (20));
System.out.println ("Is the key '5' present?" + l.containsKey (5));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
Is the key '20' present? true
Is the key '5' present? false
  1. get (Object K): It is used to get the value of a particular key from the map.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value is: " + l.get (20));
System.out.println ("The value is: " + l.get (25));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value is: Python
The value is: JavaScript
  1. removeEldestEntry (Map.Entry eldest): This method is used to remove the eldest entry from the map.
Example:
import java.util.*;
public class LinkedDemo {
private static final int MAX = 5;
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>()
{
protected boolean removeEldestEntry (Map.Entry<Integer, String> eldest)
 {
return size () > MAX;
}
};
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
l.put (5, "Angularjs");
System.out.println ("" + l);
l.put (6, "Reactjs");
System.out.println ("" + l);
} }
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}
{1=HTML, 2=Python, 3=JavaScript, 4=C++, 5=Angularjs}
{2=Python, 3=JavaScript, 4=C++, 5=Angularjs, 6=Reactjs}
  1. forEach (Biconsumer<K, V> action): It completes the given action of each entry in the map until all entries have been processed throws the exception.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
for (Map.Entry m: l.entrySet ()){ 
System.out.println (m.getKey () +" "+m.getValue ()); 
}  } }
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}
0 Java
1 HTML
2 Python
3 JavaScript
4 C++
6. keySet (): It returns the set of keys in the map. Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Keys: "+l.keySet ());    }}
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}
Keys: [0, 1, 2, 3, 4]
  1. Values (): This returns the collections of values in the map.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Values: "+l.values ());     } }
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}

Values: [Java, HTML, Python, JavaScript, C++]
  1. entrySet (): This returns the Set view of the whole mapping in the map.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String> ();
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Key-Value pairs: "+l.entrySet ());   
} }
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}
Key-Value pairs: [0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++]
  1. getOrDeafult (Object key, V default Values): This method returns the value which is mapped with particular key. The default value if map cannot contain the mapping of the key.
Example:
import java.util.*;
public class LinkedDemo {
public static void main (String [] args) {
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>();
l.put (0, "Java");
l.put (1, "HTML");
l.put (2, "Python");
l.put (3, "JavaScript");
l.put (4, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Value pairs by default: "+l.getOrDefault (7,"json"));  
System.out.println ("Value pairs by default: "+l.getOrDefault (3,""));
} }
Output:
Initial Mappings are: {0=Java, 1=HTML, 2=Python, 3=JavaScript, 4=C++}
Value pairs by default: json
Value pairs by default: JavaScript

Related Topics

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to sort an array in Java

Sorting is the process of arranging the elements of a list or array in a specific order, either ascending or descending. The sorting criterion numerical and alphabetical is commonly used...

6 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.