7:26 pm - Thursday August 17, 2017

check whether Hashset is empty using boolean isEmpty()

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  Hashset is empty using boolean isEmpty() method.

Returns True if this set contains no elements.

To check hashset whether empty or not and return result in Boolean value.

Example

import java.util.HashSet;
public class CheckEmpty
{
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);
   System.out.println(hs);
   System.out.println("Is HashSet empty? "+hs.isEmpty());
   hs.remove("third");
   System.out.println(hs);
}
}

Output

          [5, 6, 23, 12]

         Is HashSet empty? false

         [5, 6, 23, 12]

BACK

Filed in: Java Examples

Comments are closed.