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
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.add("Hello");
set1.add("Java");
HashSet
set2.add("World");
set2.add("Java");
set1.retainAll(set2) ; // Set1 stores the intersection of two sets
for (String s : set1) {
System.out.println(s);
}
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.add("Hello");
HashSet
set2.add("World");
set1.addAll(set2); //set1 stores the union of two sets
for (String s : set1) {
System.out.println(s);
}
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.add("Hello");
set1.add("Java");
HashSet
set2.add("World");
set2.add("Java");
set1.removeAll(set2) ; // Set1 stores the difference between the two sets
for (String s : set1) {
System.out.println(s);
}
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.add("Hello");
set1.add("Java");
List
Collections.sort(list); // Sort the elements in the list
for (String s : list) {
System.out.println(s);
}
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!