beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Check if a particular key exists in HashMap example

By Chaitanya Singh | Filed Under: Java Collections

In the last tutorial we learnt how to check whether a particular value exists in HashMap. In this example we are gonna see how to check if a particular key is present in HashMap. We will be using containsKey() method of HashMap class to perform this check. The method definition and description are as follows:

public boolean containsKey(Object key): Returns true if this map contains a mapping for the specified key.

Example

The steps we followed in the below example are:

1) Create a HashMap and populate it with key-value pairs.
2) Check any key existence by calling containsKey() method. This method returns a boolean value.

import java.util.HashMap;

public class CheckKeyExample {
 
 public static void main(String[] args) {
 
    // Creating a HashMap of int keys and String values
    HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
 
    // Adding Key and Value pairs to HashMap
    hashmap.put(11,"Chaitanya");
    hashmap.put(22,"Pratap");
    hashmap.put(33,"Singh");
    hashmap.put(44,"Rajesh");
    hashmap.put(55,"Kate");
 
    // Checking Key Existence
    boolean flag = hashmap.containsKey(22);
    System.out.println("Key 22 exists in HashMap? : " + flag);
 
    boolean flag2 = hashmap.containsKey(55);
    System.out.println("Key 55 exists in HashMap? : " + flag2);
 
    boolean flag3 = hashmap.containsKey(99);
    System.out.println("Key 99 exists in HashMap? : " + flag3);
 }
}

Output:

Key 22 exists in HashMap? : true
Key 55 exists in HashMap? : true
Key 99 exists in HashMap? : false

Enjoyed this post? Try these related posts

  1. Difference between list set and map in java?
  2. Java ArrayList set() Method example
  3. Remove all mappings from Hashtable example – Java
  4. Java – LinkedList Iterator example
  5. Java – Get sub List from LinkedList example
  6. Java Iterator with examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap