Home > Java > javaTutorial > body text

Use java's HashMap.containsKey() function to determine whether the HashMap contains the specified key

王林
Release: 2023-07-26 08:21:12
Original
1380 people have browsed it

Use Java's HashMap.containsKey() function to determine whether the HashMap contains the specified key

In Java, HashMap is a commonly used data structure that stores data in the form of key-value pairs, where each Each key is unique. When we need to find whether a key exists in a HashMap, we can use the containsKey() function to determine.

containsKey() function is a member function of the HashMap class. Its function is to determine whether the HashMap contains the specified key. Its function signature is as follows:

public boolean containsKey(Object key)

This function accepts a parameter key, which represents the key to be found. If the key is contained in the HashMap, returns true; otherwise, returns false.

The following is a sample code using the containsKey() function:

import java.util.HashMap;

public class Main {

public static void main(String[] args) {
    // 创建一个HashMap对象并添加一些键值对
    HashMap<String, Integer> hashMap = new HashMap<>();
    hashMap.put("apple", 1);
    hashMap.put("banana", 2);
    hashMap.put("orange", 3);

    // 判断HashMap中是否含有指定的键
    String key = "apple";
    if (hashMap.containsKey(key)) {
        System.out.println(key + " is in the HashMap");
    } else {
        System.out.println(key + " is not in the HashMap");
    }

    key = "pear";
    if (hashMap.containsKey(key)) {
        System.out.println(key + " is in the HashMap");
    } else {
        System.out.println(key + " is not in the HashMap");
    }
}
Copy after login

}

Run the above code, the output result is as follows:

apple is in the HashMap
pear is not in the HashMap

In the sample code, we first create a HashMap Object hashMap and added three key-value pairs to it using the put() function. Then, we determine whether the HashMap contains the specified key by calling the containsKey() function.

In the first judgment, we set the key to "apple". Because the key exists in the HashMap, the judgment result is true and "apple is in the HashMap" is printed.

In the second judgment, we set the key to "pear". Because the key does not exist in the HashMap, the judgment result is false and "pear is not in the HashMap" is printed.

To summarize, using Java's HashMap.containsKey() function can easily determine whether the HashMap contains the specified key. This can help us quickly find the corresponding value or determine whether the key exists when we need to find a key, improving the efficiency and accuracy of the program.

The above is the detailed content of Use java's HashMap.containsKey() function to determine whether the HashMap contains the 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