Home Java javaTutorial How to use the HashSet function for set operations in Java

How to use the HashSet function for set operations in Java

Jun 26, 2023 pm 05:15 PM
java hashset Set operations

The HashSet function in Java is a collection class implemented based on a hash table. Since it is a collection class, it naturally has the function of collection operations. This article will introduce how to use the HashSet function to perform collection operations.

1. Definition and declaration of HashSet

HashSet is a collection class, so you need to import the Java.util package first.

import java.util.HashSet;

Then you can create a HashSet instance:

HashSet set = new HashSet<>();

In this example, we create a HashSet instance of type String, "set" is the name of this instance, so that we can call its method.

2. Add elements

HashSet adds elements through the add() method. If you want to add a string to the set, you can write like this:

set.add("Hello");

If you want to add multiple elements, you can write like this :

set.add("Hello");
set.add("World");

This will add two elements to the collection.

3. Delete elements

When deleting elements in HashSet, we can use the remove() method to achieve it. If you want to delete a string, you can write:

set.remove("Hello");

In this way we delete an element from the set. Of course, we can also delete multiple elements:

set.remove("Hello");
set.remove("World");

In this way we successfully remove the elements from the collection Two elements have been deleted.

4. Determine whether the element exists

When HashSet determines whether the element exists, we can use the contains() method to achieve this. If you want to determine whether a string exists in the collection, you can write like this:

boolean isExist = set.contains("Hello");

This way we can know "Hello" Whether this element exists in the collection.

5. Traversing elements

Traversing elements is also an important function of the HashSet function. We can do this through a for-each loop.

for (String str : set) {
System.out.println(str);
}

This way we can output all elements in the set in sequence.

6. Find the intersection of sets

If you want to ask for the intersection of two sets, you need to use the retainAll() method of HashSet.

HashSet set1 = new HashSet<>();
set1.add("Hello");
set1.add("Java");

HashSet set2 = new HashSet<>();
set2.add("World");
set2.add("Java");

set1.retainAll(set2) ; // Set1 stores the intersection of two sets
for (String s : set1) {

1

System.out.println(s);

Copy after login
Copy after login
Copy after login
Copy after login

}

In this example, we first create two sets set1 and set2, and then store their intersection in set1. Finally, all elements in set1 are output through the for-each loop.

7. Find the union of sets

If you want to ask for the union of two sets, you need to use the addAll() method of HashSet.

HashSet set1 = new HashSet<>();
set1.add("Hello");

HashSet set2 = new HashSet<>( );
set2.add("World");

set1.addAll(set2); //set1 stores the union of two sets
for (String s : set1) {

1

System.out.println(s);

Copy after login
Copy after login
Copy after login
Copy after login

}

In this example, we first create two sets set1 and set2, and then store their union in set1. Finally, all elements in set1 are output through the for-each loop.

8. Find the difference of sets

If you want to find the difference of two sets, you need to use the removeAll() method of HashSet.

HashSet set1 = new HashSet<>();
set1.add("Hello");
set1.add("Java");

HashSet set2 = new HashSet<>();
set2.add("World");
set2.add("Java");

set1.removeAll(set2) ; // Set1 stores the difference between the two sets
for (String s : set1) {

1

System.out.println(s);

Copy after login
Copy after login
Copy after login
Copy after login

}

In this example, we first created two sets set1 and set2, and then store their difference in set1. Finally, all elements in set1 are output through the for-each loop.

9. Sorting of Set Elements

HashSet is an unordered collection. If you want to sort the elements in the collection, you can use the Sort() method in the Java.util package.

HashSet set1 = new HashSet<>();
set1.add("Hello");
set1.add("Java");

List list = new ArrayList<>(set1);
Collections.sort(list); // Sort the elements in the list
for (String s : list) {

1

System.out.println(s);

Copy after login
Copy after login
Copy after login
Copy after login

}

In this example, we first create an unordered HashSet collection set1, then convert it into an ordered List collection, and then sort the elements in the List collection. Finally, all elements in the sorted List collection are output through the for-each loop.

Summary

HashSet is a collection class implemented based on hash table. It has powerful functions and can realize the addition, deletion, judgment, traversal, intersection, union, and collection of collections. Various operations such as difference set and sorting. The above operations can provide developers with convenience and perform Java programming more efficiently.

The above is the detailed content of How to use the HashSet function for set operations in Java. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles