Java HashSet

A HashSet, based on a HashMap, is a collection of no duplicate elements.

A HashSet allows null values.

A HashSet is unordered, that is, the order of the inserts is not recorded.

A HashSet is not thread-safe, and if multiple threads attempt to modify a HashSet at the same time, the end result is indeterminate. You must explicitly synchronize concurrent access to the HashSet when accessing multiple threads.

HashSet implements the Set interface.

Elements in a HashSet are actually objects, and some common primitive types can use their wrapper classes.

The table of packing classes corresponding to basic types is as follows:

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

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

import java.util.HashSet; // Introduce the HashSet classCopy the code

In the following example, we create a HashSet object sites to hold string elements:

HashSet<String> sites = new HashSet<String>();
Copy the code

Add elements

The HashSet class provides many useful methods for adding elements using the add() method:

The instance

// Introduce the 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 addedSystem.out.println(sites); }}Copy the code

After executing the above code, the output is as follows:

[Google, Runoob, Zhihu, Taobao]
Copy the code

In the above example, Runoob is added twice, and it only appears once in the collection, because each element in the collection must be unique.

Determines whether the element exists

We can use the contains() method to determine if an element exists in a set:

The instance

// Introduce the 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")); }}Copy the code

After executing the above code, the output is as follows:

true
Copy the code

Remove elements

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

The instance

// Introduce the 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 elements, return true on success, false otherwiseSystem.out.println(sites); }}Copy the code

After executing the above code, the output is as follows:

[Google, Runoob, Zhihu]
Copy the code

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

The instance

// Introduce the 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 addedsites.clear(); System.out.println(sites); }}Copy the code

After executing the above code, the output is as follows:

[]
Copy the code

Calculate the size

To count the number of elements in a HashSet, use the size() method:

The instance

// Introduce the 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 addedSystem.out.println(sites.size()); }}Copy the code

After executing the above code, the output is as follows:

4
Copy the code

Iterative HashSet

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

The instance

// Introduce the 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); }}}Copy the code

After executing the above code, the output is as follows:

Google Runoob Zhihu Taobao sites.add("Taobao"); sites.add("Zhihu"); sites.add("Runoob"); For (String I: sites) {system.out.println (I); }}}Copy the code

After executing the above code, the output is as follows:

Google
Runoob
Zhihu
Taobao
Copy the code