1:40 am - Wednesday February 22, 2017

Get size of Hashset using int size()

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 Get size of Hashset using  int size() method.

Returns the number of elements in this set (its cardinality).

 Whenever we call this method, it checks number of elements in hash table.

Example

import java.util.HashSet;

public class CheckSize

{
    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"));

    }
}

Output

           Is HashSet empty? false

           before Remove Size the HashSet: 4

          [5, 23, 12]

           After remove Size of the HashSet: 3

BACK

Filed in: Java Examples

Comments are closed.