12:00 pm - Sunday June 25, 2017

check whether a value is present in Hashset boolean contains(Object value)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to check whether a value is present in Hashset using boolean contains() method.

True if this set contains the specified element.

 To check  whether the element available in hash and return possible boolean value.

 We can check the specific elements available in hashset.

Example

import java.util.HashSet;
public class CheckContains

{

 public static void main(String a[])

{
        HashSet<Integer> hs = new HashSet<Integer>();        

        hs.add(12);

        hs.add(23);

        hs.add(5);

        hs.add(6);

        hs.add(6);

        hs.add(6);

        System.out.println(hs);

        System.out.println("Is HashSet empty? "+hs.isEmpty());

        System.out.println("before Remove Size the HashSet: "+hs.size());

        hs.remove(6);

        System.out.println(hs);            

        System.out.println("After remove Size of the HashSet: "+hs.size());

        System.out.println("Does HashSet contains first element? "+hs.contains("first"));

        System.out.println("Does HashSet contains first element? "+hs.contains(6));

        System.out.println("Does HashSet contains first element? "+hs.contains(12));
    }

}

Output

         After remove Size of the HashSet: 3

          Does HashSet contains first element? false

         Does HashSet contains first element? false

         Does HashSet contains first element? true

BACK

Filed in: Java Examples

Comments are closed.