Home > Java > javaTutorial > body text

How to use set in java

下次还敢
Release: 2024-05-07 03:30:31
Original
651 people have browsed it

In Java, Set is a data structure used to store unique elements to ensure the uniqueness of the elements. Sets can be created with HashSet, LinkedHashSet and TreeSet. You can add, remove elements, and use other operations such as contains(), isEmpty(), and size(). Set is suitable for scenarios where you need to store unique data, quickly find whether an element exists, sort data, or maintain insertion order.

How to use set in java

Usage of Set in Java

Set is an important data structure in Java Collections Framework. It stores Repeating elements. Set guarantees the uniqueness of its elements, so it is useful when you need to deal with non-duplicate data.

Creating a Set

In Java, you can create a Set using the following methods:

  • HashSet: Unordered And efficient Set
  • LinkedHashSet: An ordered Set that maintains insertion order
  • TreeSet: An ordered and sorted according to the natural order of the elements Set

Add elements

You can add elements to Set through the add() method. If the element already exists, it will not be added to the Set.

Remove elements

Use the remove() method to remove elements from a Set. If the element exists, it is removed from the Set.

Other operations

In addition to adding and removing elements, Set also provides many other operations, such as:

  • contains (): Check whether the Set contains a specific element
  • isEmpty(): Check whether the Set is empty
  • size(): Get the number of elements in the Set
  • clear(): Clear all elements in the Set
  • addAll(): Add the elements in another set All elements are added to the Set
  • removeAll(): Remove all elements in another collection from the Set

Use case

Set is very useful in the following scenarios:

  • When you need to store unique data, such as a unique ID or email address
  • When you need to quickly find whether an element When present
  • When you need to sort the data or maintain insertion order

Example

The following is a simple example using a HashSet:

<code>import java.util.HashSet;

public class SetExample {

    public static void main(String[] args) {
        // 创建一个 HashSet
        HashSet<String> names = new HashSet<>();

        // 向 Set 中添加元素
        names.add("John");
        names.add("Mary");
        names.add("Bob");

        // 检查 Set 中是否包含元素
        System.out.println(names.contains("John")); // true

        // 获取 Set 中的元素数量
        System.out.println(names.size()); // 3

        // 打印 Set 中的所有元素
        for (String name : names) {
            System.out.println(name);
        }
    }
}</code>
Copy after login

The above is the detailed content of How to use set 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template