Java HashSet : size() contains() remove() clear()

The remove() method shall be used to remove an unwanted object from a collection. We shall use the clear() method to completely empty a collection.

The Contains() method enables us to determine whether a specific object is present in a collection or not.

The size() method returns the number of elements in a collection.

The clear() method clear the all elements in a collection.

Program

0

 

 

 

Program Source

import java.util.HashSet;

public class Javaapp {

    public static void main(String[] args) {
        
        HashSet hs = new HashSet();
        hs.add(20);
        hs.add(40);
        hs.add(60);
        hs.add(80);
        System.out.println("hs : "+hs);
        System.out.println("hs size : "+hs.size());
        
        if(hs.contains(30))
        {
            System.out.println("Yes 30 contain");
        }else
            System.out.println("30 not contain");
        
        hs.remove(80);
        System.out.println("hs : "+hs);
        
        hs.clear();
        System.out.println("hs : "+hs);
    }
}

Leave a Comment