×

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 inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

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.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.