Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetContainsExample3 { public static void main(String[] args) { //Create HashSet object HashSet
hashset = new HashSet
(); //Adding elements to HashSet hashset.add("Facebook"); hashset.add("Twitter"); hashset.add("Instagram"); //Print the elements of the HashSet System.out.println("Elements of the HashSet:"); for(String val : hashset) { System.out.println(val); } //Check if the hashSet contains "Whatsapp" System.out.println("Does the HashSet contains 'Whatsapp'? :- "+hashset.contains("Whatsapp")); //Check if the hashSet contains "Facebook" System.out.println("Does the HashSet contains 'Facebook'? :- "+hashset.contains("Facebook")); } }
Output