Java SeriesHashSet

HashSet

  • introduce
    • Add element
    • Determine whether the element exists
    • Delete element
    • To delete all elements in the collection, use the clear method:
    • Calculate size
    • Iterate over a HashSet
  • Series of articles
  • version record


Introduction

HashSet is implemented based on HashMap and is a set that does not allow duplicate elements.
HashSet allows null values.
HashSet is unordered, that is, the order of insertion is not recorded.
HashSet is not thread-safe, if multiple threads try to modify the HashSet simultaneously, the final result is undefined. You must explicitly synchronize concurrent access to a HashSet when accessed by multiple threads.
HashSet implements the Set interface.

The elements in HashSet are actually objects, and some common basic types can use its wrapper classes.

The packaging class table corresponding to the basic types is as follows:

Basic type Reference type
boolean Boolean
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character

The HashSet class is located in the java.util package and needs to be introduced before use. The syntax format is as follows:

import java.util.HashSet; //Introduce the HashSet class
In the following example, we create a HashSet object sites to save string elements:

HashSet sites = new HashSet();

Add element

The HashSet class provides many useful methods. To add elements, you can use the add() method:

Example

//Introduce HashSet class
import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        System.out.println(sites);
    }
}

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

[Google, Runoob, Zhihu, Taobao]
In the example above, Runoob is added twice, and it will only appear once in the collection because every element in the collection must be unique.

Determine whether the element exists

We can use the contains() method to determine whether an element exists in the collection:

Example

//Introduce HashSet class
import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        System.out.println(sites.contains("Taobao"));
    }
}

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

true

Delete element

We can use the remove() method to remove elements from the collection:

Example
//Introduce HashSet class

import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        sites.remove("Taobao"); // Delete the element. If the deletion is successful, it will return true, otherwise it will be false.
        System.out.println(sites);
    }
}

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

[Google, Runoob, Zhihu]

To delete all elements in the collection, you can use the clear method:

Example

//Introduce HashSet class
import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        sites.clear();
        System.out.println(sites);
    }
}

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

[]

Calculate size

If you want to count the number of elements in a HashSet, you can use the size() method:

Example

//Introduce HashSet class
import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        System.out.println(sites.size());
    }
}

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

4

Iterate over HashSet

You can use for-each to iterate over the elements in a HashSet.

Example

//Introduce HashSet class
import java.util.HashSet;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob"); // Duplicate elements will not be added
        for (String i : sites) {<!-- -->
            System.out.println(i);
        }
    }
}

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

Google
Runoob
Zhihu
Taobao

Series of articles

Content Address link
JAVA series Java Introduction
JAVA Series Java Basics
JAVA Series ArrayList
JAVA Series LinkedList


================================================== =======================


If you are interested in this series of articles, please continue to follow the blogger’s updates. The blogger will continue to output high-quality content

?

Bloggers need everyone’s support. Your support is the inexhaustible motivation for my creation



?

~ Like and collect + follow~



================================================== =======================

Version record

  • 2023-10-27 First Edition