In Java programming, the HashSet class is a commonly used data structure, which is used to store a collection of unique elements. In the HashSet class, the clear() method can clear all elements in the HashSet collection. This article will explain in detail the usage of the clear() method of the HashSet class and provide specific code examples.
1. Introduction to the clear() method of the HashSet class
In Java, the HashSet class is an implementation based on a hash table. It implements the Set interface and inherits from the AbstractSet class. The HashSet class can store a collection of unique elements, and it does not guarantee the order of the elements. The HashSet class provides many methods for operations such as adding, removing, and checking elements. Among them, the clear() method is used to clear all elements in the HashSet collection.
The syntax format of the clear() method of the HashSet class is as follows:
public void clear()
In the parentheses after the method name, no parameters need to be passed in. After calling the clear() method, all elements in the HashSet collection will be cleared and the collection size will become 0.
2. Usage examples of the clear() method of the HashSet class
In order to better understand the usage of the clear() method of the HashSet class, we will provide some specific code examples next.
1. Use the clear() method to clear the HashSet collection
We first define a HashSet collection and add some elements to it. Then use the clear() method to clear the elements in the collection and check whether the collection is empty.
import java.util.HashSet; public class HashSetDemo { public static void main(String[] args) { HashSet<String> animalSet = new HashSet<String>(); // 向集合中添加元素 animalSet.add("dog"); animalSet.add("cat"); animalSet.add("tiger"); animalSet.add("lion"); System.out.println("HashSet集合大小为:" + animalSet.size()); // 使用clear()方法清空集合 animalSet.clear(); System.out.println("HashSet集合清空后大小为:" + animalSet.size()); } }
Output result:
HashSet集合大小为:4 HashSet集合清空后大小为:0
From the output result, we can see that after using the clear() method to clear the HashSet collection, the collection size becomes 0, indicating that all elements in the collection have been Cleared.
2. Use the clear() method to clear the HashSet collection, and then add elements to it
We can add elements to the HashSet collection after clearing it.
import java.util.HashSet; public class HashSetDemo { public static void main(String[] args) { HashSet<String> animalSet = new HashSet<String>(); // 向集合中添加元素 animalSet.add("dog"); animalSet.add("cat"); animalSet.add("tiger"); animalSet.add("lion"); System.out.println("HashSet集合大小为:" + animalSet.size()); // 使用clear()方法清空集合 animalSet.clear(); System.out.println("HashSet集合清空后大小为:" + animalSet.size()); // 再次向集合中添加元素 animalSet.add("monkey"); animalSet.add("rabbit"); System.out.println("HashSet集合添加元素后大小为:" + animalSet.size()); } }
Output results:
HashSet集合大小为:4 HashSet集合清空后大小为:0 HashSet集合添加元素后大小为:2
From the output results, we can see that after using the clear() method to clear the HashSet collection, and then adding elements to it, the collection only contains newly added elements. , indicating that the original elements have been cleared.
3. Conclusion
This article explains in detail the usage of the clear() method of the HashSet class in Java and provides specific code examples. We can use the clear() method to clear all elements in the HashSet collection and then add new elements to it.
The above is the detailed content of Interpretation of Java documentation: Detailed explanation of the usage of the clear() method of the HashSet class. For more information, please follow other related articles on the PHP Chinese website!