Home > Java > javaTutorial > body text

How to use HashMap.containsKey() method in Java to check whether a map contains a specified key?

WBOY
Release: 2023-11-18 11:49:23
Original
1281 people have browsed it

How to use HashMap.containsKey() method in Java to check whether a map contains a specified key?

In Java, HashMap is a commonly used data structure, which provides a way to map keys and values. In actual development, we often need to check whether the HashMap contains a specific key. To meet this need, Java provides the HashMap.containsKey() method.

The HashMap.containsKey() method is used to check whether the HashMap contains the specified key. It accepts a key as a parameter and returns a boolean value indicating whether the HashMap contains the key. The following is the sample code of this method:

import java.util.HashMap;

public class HashMapExample {
    public static void main(String[] args) {
        // 创建一个HashMap对象
        HashMap<String, Integer> hashMap = new HashMap<>();

        // 向HashMap中添加键值对
        hashMap.put("apple", 1);
        hashMap.put("banana", 2);
        hashMap.put("orange", 3);

        // 检查HashMap中是否包含指定的键
        System.out.println(hashMap.containsKey("apple"));   // true
        System.out.println(hashMap.containsKey("watermelon"));   // false
    }
}
Copy after login

In the above sample code, we first create a HashMap object, and then use the put() method to add three sets of key-value pairs to the HashMap. Next, we use the containsKey() method to check whether the specified key is contained in the HashMap. For existing keys, the containsKey() method returns true, otherwise it returns false.

In the above example code, we checked whether the HashMap contains the keys "apple" and "watermelon" through the containsKey() method. When running the program, we can see that the first output is true and the second output is false.

To summarize, using the HashMap.containsKey() method in Java can easily check whether the HashMap contains the specified key. We just need to call the method and pass in the key to be checked as a parameter to get the result. This method is very useful in daily development work and can help us better handle key-value pairs in HashMap.

The above is the detailed content of How to use HashMap.containsKey() method in Java to check whether a map contains a specified key?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!