- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a particular key exists in Java LinkedHashMap
Use the containsKey() method to check whether a particular key exists in LinkedHashMap or not.
Create a LinkedHashMap −
LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus");
Now, let’s say we need to check whether key 4 exists or not. For that, use the containsKey() method like this −
l.containsKey(4)
The following is an example to check if a particular key exists in LinkedHashMap −
Example
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap<Integer, String> l = new LinkedHashMap<Integer, String>(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus"); System.out.println("LinkedHashMap elements..."); System.out.println(l); System.out.println("
Does key 4 exist in the LinkedHashMap elements? "+l.containsKey(4)); } }
Output
LinkedHashMap elements... {1=Mars, 2=Earth, 3=Jupiter, 4=Saturn, 5=Venus} Does key 4 exist in the LinkedHashMap elements? True
- Related Articles
- Check if a particular value exists in Java LinkedHashMap
- Java Program to check if a particular key exists in TreeMap
- Check if a particular value exists in Java TreeSet
- Check if a particular element exists in Java LinkedHashSet
- Check if a given key exists in Java HashMap
- Java Program to check if a particular element exists in HashSet
- Java Program to check if a particular value exists in TreeMap
- How to check if a key exists in a Python dictionary?
- How to check if a key exists in a map in Golang?
- How to Check if a Key Exists in the Hashtable in C#?
- Check if a file exists in Java\n
- How to check if a given key already exists in a Python dictionary?
- Check if a Point Exists in Circle Sector in Java
- Checking if a key exists in a JavaScript object
- How to check if a file exists or not in Java?

Advertisements