Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetAddExample3 { public static void main(String[] args) { //Create HashSet object HashSet
hashset1 = new HashSet
(); //Adding elements to HashSet hashset1.add("Facebook"); hashset1.add("Twitter"); hashset1.add("Instagram"); boolean flag = hashset1.add("Google"); System.out.println("Is the insertion successful?? - "+flag); //Print the elements of the HashSet System.out.println("Elements of the HashSet:"); for(String val : hashset1) { System.out.println(val); } } }
Output