Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetClearExample3 { public static void main(String[] args) { //Create HashSet object HashSet
hset1 = new HashSet
(); //Add elements to HashSet hset1.add("Facebook"); hset1.add("Twitter"); hset1.add("Instagram"); hset1.add("Google"); //Print the elements System.out.println("Size of the HashSet: "+hset1.size()); System.out.println("Elements of the HashSet before clear:"); for(String val : hset1) { System.out.println(val); } hset1.clear(); //Printing the elements after clear operation System.out.println("Elements of the HashSet after clear:"); System.out.println("Size of the hashset: "+hset1.size()); for(String val : hset1) { System.out.println(val); } } }
Output