Home > Java > javaTutorial > Use the contains() method of the TreeSet class to determine whether an element exists in Java

Use the contains() method of the TreeSet class to determine whether an element exists in Java

WBOY
Release: 2023-07-27 10:33:21
Original
1017 people have browsed it

Use the contains() method of the TreeSet class to determine whether an element exists in Java

TreeSet is an ordered set in Java. It is sorted according to the natural order of the elements and retains this ordering. In actual development, we often need to determine whether an element exists in a TreeSet. Fortunately, Java's TreeSet class provides a contains() method to accomplish this task.

Let's look at a specific example to demonstrate how to use the contains() method of the TreeSet class to determine whether an element exists in Java.

First, we need to create a TreeSet object, then add some elements to it, and finally use the contains() method to determine whether an element exists in the set. The following is a complete code example:

import java.util.TreeSet;

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

        // 向集合中添加元素
        set.add("Apple");
        set.add("Banana");
        set.add("Orange");
        set.add("Grape");
        set.add("Watermelon");

        // 使用contains()方法判断集合中是否存在某个元素
        if (set.contains("Orange")) {
            System.out.println("集合中存在元素 Orange");
        } else {
            System.out.println("集合中不存在元素 Orange");
        }

        if (set.contains("Mango")) {
            System.out.println("集合中存在元素 Mango");
        } else {
            System.out.println("集合中不存在元素 Mango");
        }
    }
}
Copy after login

Run the above code, the output results are as follows:

集合中存在元素 Orange
集合中不存在元素 Mango
Copy after login

Through the above example code, we can see that by calling the contains() method of the TreeSet class, we can Easily determine whether an element exists in a collection. If it exists, it returns true; if it does not exist, it returns false.

It should be noted that the contains() method can only determine whether an element exists in the collection, but cannot determine the position of the element in the collection. If we need to find the position of an element in a collection, we can use other methods to achieve this.

To summarize, using the contains() method of the TreeSet class can easily determine whether an element exists in Java. This is a very commonly used method in Java collection classes, which can help us improve development efficiency. Hope this article can be helpful to you!

The above is the detailed content of Use the contains() method of the TreeSet class to determine whether an element exists in Java. For more information, please follow other related articles on the PHP Chinese website!

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