×

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

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.