在这里,我们将看到如何一次性删除HashSet
的所有元素。我们可以通过调用HashSet
类的clear()
方法来实现。
示例
import java.util.HashSet;
class EmptyHashSetExample{
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hset = new HashSet<String>();
//add elements to HashSet
hset.add("Element1");
hset.add("Element2");
hset.add("Element3");
hset.add("Element4");
hset.add("Element5");
// Display HashSet elements
System.out.println("Before: HashSet contains: "+ hset);
/* public void clear(): It removes all the elements
* from HashSet. The set becomes empty after this
* method gets called.
*/
hset.clear();
// Display HashSet content again
System.out.println("After: HashSet contains: "+ hset);
}
}
输出:
Before: HashSet contains: [Element1, Element2, Element3, Element4, Element5]
After: HashSet contains: []