Check if a particular key exists in LinkedHashMap

In this tutorial, we will learn how to Check if a particular key exists in LinkedHashMap in Java ?

LinkedHashMap in Java is Hash table and Linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. It maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating through a collection-view of a LinkedHashMap, the elements will be returned in the order in which they were inserted. To access a value in LinkedHashMap, you need to have a key.

To check if a particular key exists in LinkedHashMap or not, LinkedHashMap class in Java provides a method containsKey(). We will use this method to check if particular key exists in LinkedHashMap. The syntax of this method is:

Syntax of containsKey() method:

Parameters:
key – The key whose presence in this map is to be tested.

Returns:
true if the key exists in map.

Example:

 

Output:

 

Related posts

Leave a Comment