How to check if a particular element exists in HashSet in Java

1 Answer

0 votes
package javaapplication1;

import java.util.HashSet;

public class JavaApplication1 {

    public static void main(String[] args) {

        HashSet hash = new HashSet();
   
        hash.add(new Integer("1"));
        hash.add(new Integer("2"));
        hash.add(new Integer("3"));
        hash.add(new Integer("4"));
        hash.add(new Integer("5"));

        boolean b = hash.contains(new Integer("4"));
        System.out.println(b);
  }
}
  
/*
run:

true

*/

 



answered Sep 26, 2016 by avibootz
...