Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetContainsExample2 { public static void main(String[] args) { //Get HashMap object from method init() @SuppressWarnings("rawtypes") HashSet studentSet = init(); //Ask input from console System.out.print("Enter student name:"); Scanner s = new Scanner(System.in); String name = s.nextLine(); s.close(); //Check if the console input is in the student database if(studentSet.contains(name)){ System.out.println(name +" found on student list."); } else{ System.out.println(name +" name not on the student list."); } } @SuppressWarnings("rawtypes") private static HashSet init() { //CreateHashSet object HashSet
studentSet = new HashSet<>(); //Add values to HashMap studentSet.add("Rishi"); studentSet.add("Sharon"); studentSet.add("Pawan"); return studentSet; } }
Output