English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java other topics

Usage and examples of Java HashMap containsKey()

Java HashMap Methods

The Java HashMap containsKey() method checks if there is a mapping for the specified key in the hash map.

The syntax of containsKey() method is:

hashmap.containsKey(Object key)

containsKey() parameter

  • key -  Check for key mapping in hash map

Returns value of containsKey()

  • Returns true if there is a mapping for the specified key in the hash map

  • Returns false if there is no mapping for the specified key in the hash map

Example1: Java HashMap containsKey()

import java.util.HashMap;
class Main {
  public static void main(String[] args){
    //Create HashMap
    HashMap<String, String> details = new HashMap<>();
    //Add the mapping to the HashMap
    details.put("Name", "w3codebox");
    details.put("Domain", "oldtoolbag.com");
    details.put("Location", "Nepal");
    System.out.println("w3codebox details: 
" + details);
    //Check if the key "Domain" exists
    if(details.containsKey("Domain")) {
      System.out.println("Domain exists in Hashmap");
    }
  }
}

Output Result

w3codebox details: 
{Domain=oldtoolbag.com, Name=w3codebox, Location=Nepal}
Domain exists in Hashmap

In the above example, we created a hash map. Note these expressions,

details.containsKey("Domain") // Return true

  Here, hashmap contains a mapping with the key of Domain. Therefore, the if statement block is executed, and the containsKey() method will return true and the statement.

Example2:If the key does not exist, add the entry to the HashMap

import java.util.HashMap;
class Main {
  public static void main(String[] args){
    // Create a HashMap
    HashMap<String, String> countries = new HashMap<>();
    // Add the mapping to the HashMap
    countries.put("USA", "Washington");
    countries.put("Australia", "Canberra");
    System.out.println("HashMap:\n" + countries);
    // Check if the key "Spain" exists
    if(!countries.containsKey("Spain")) {
      // If the key does not exist, add the entry
      countries.put("Spain", "Madrid");
    }
    System.out.println("Updated HashMap:\n" + countries);
  }
}

Output Result

HashMap:
{USA=Washington, Australia=Canberra}
Updated HashMap:
{USA=Washington, Australia=Canberra, Spain=Madrid}

In the above example, please note the following expression:

if(!countries.containsKey("Spain")) {..}

Here, we used the containsKey() method to check if there is a mapping for Spain in the hash map. Since we used the negation symbol (!), if this method returns false, the if block will be executed.

 Therefore, a new mapping is only added if there is no specified key mapping in the hashmap.

Note: We can also useHashMap putIfAbsent()Methods perform the same task.

Java HashMap Methods