How to Check If LinkedHashMap contains Particular Key in Java


In this example we will show how to check if LinkedHashMap object contains a particular key using containsKey method of LinkedHashMap class.

Source Code

package com.beginner.examples;

import java.util.LinkedHashMap;

public class CheckKeyOfLinkedHashMap {

	public static void main(String[] args) {
		
		//create LinkedHashMap.
		LinkedHashMap linkedHashMap = new LinkedHashMap();
		
		linkedHashMap.put("1","Alice");
		linkedHashMap.put("2","Bob");
		linkedHashMap.put("3","Jack");
		
		/*
		 *  To check whether a particular key exists in LinkedHashMap use containsKey(Object key) method of LinkedHashMap. 
		 */
		boolean result = linkedHashMap.containsKey("3");
		System.out.println("3 exists in LinkedHashMap ? : " + result);
	}

}

Output:

3 exists in LinkedHashMap ? : true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments