Home » Core Java » util » LinkedHashMap » Check key existence in LinkedHashMap example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Check key existence in LinkedHashMap example

This is an example of how to check a key existence in a LinkedHashMap. Checking if a key exists in a LinkedHashMap implies that you should:

  • Create a new LinkedHashMap.
  • Populate the linkedHashMap with elements, with put(K key, V value) API method of LinkedHashMap.
  • Invoke containsKey(Object key) API method of LinkedHashMap, with a specified key as parameter. The method returns true if this map contains a mapping for the specified key and false otherwise.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.util.LinkedHashMap;
 
public class CheckKeyLinkedHashMap {
 
  public static void main(String[] args) {
 
    // Create a LinkedHashMap and populate it with elements
    LinkedHashMap linkedHashMap = new LinkedHashMap();
    linkedHashMap.put("key_1","value_1");
    linkedHashMap.put("key_2","value_2");
    linkedHashMap.put("key_3","value_3");
 
    // boolean containsKey(Object key) returns true if the LinkedHashMap contains the key, otherwise false.
    boolean exists = linkedHashMap.containsKey("key_2");
    System.out.println("key_2 exists in LinkedHashMap ? : " + exists);
  }
}

Output:

key_2 exists in LinkedHashMap ? : true

 
This was an example of how to check a key existence in a LinkedHashMap in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments