Home  >  Article  >  Java  >  Java uses the contains() function of the HashSet class to determine whether the set contains the specified element.

Java uses the contains() function of the HashSet class to determine whether the set contains the specified element.

WBOY
WBOYOriginal
2023-07-24 12:57:171726browse

Java uses the contains() function of the HashSet class to determine whether the set contains the specified element

HashSet is one of the commonly used collection classes in Java. It can be used to store a set of non-duplicate elements. In actual development, we often need to determine whether an element exists in a HashSet. In order to facilitate judgment, the HashSet class provides the contains() function to implement the search operation for elements.

First, let’s take a look at the characteristics of HashSet. HashSet is implemented based on a hash table. It does not guarantee the order of elements and does not allow duplicate elements. HashSet uses HashMap internally to store data. Each element is used as a key of HashMap, and the value is set to a fixed Object object. Therefore, the contains() function of HashSet actually determines whether the element exists through the containsKey() function of HashMap.

Below we use a simple example to demonstrate the use of the contains() function of HashSet.

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        // 创建一个HashSet对象
        HashSet<String> set = new HashSet<>();

        // 添加元素
        set.add("apple");
        set.add("banana");
        set.add("orange");
        set.add("grape");

        // 查找元素
        System.out.println("是否包含apple:" + set.contains("apple")); // true
        System.out.println("是否包含pear:" + set.contains("pear")); // false
    }
}

In the above code, we first create a HashSet object. Then some elements are added to the collection through the add() function, and finally the contains() function is used to determine whether the collection contains the specified element.

The output results are as follows:

是否包含apple:true
是否包含pear:false

As can be seen from the output results, the contains() function returns a Boolean value. If the set contains the specified element, it returns true, otherwise it returns false. .

It should be noted that the contains() function of HashSet depends on the hashCode() and equals() functions of the element. Therefore, if we customize a class and add its objects to a HashSet, then we need to override the hashCode() and equals() functions to ensure the accuracy of the contains() function.

The above is the method and example of Java using the contains() function of the HashSet class to determine whether the set contains the specified element. Through this function, we can easily find elements in the collection, thus simplifying the development process. In practical applications, we can use this function to determine whether there are duplicate elements in the set, avoid the occurrence of duplicate data, and improve the efficiency of the program.

The above is the detailed content of Java uses the contains() function of the HashSet class to determine whether the set contains the specified element.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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