×

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

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.