Home Tutorial Java Core Hashset How to remove all element from Hashset.

 
 

Share on Google+Share on Google+
How to remove all element from Hashset.
Posted on: April 4, 2011 at 12:00 AM
Advertisement
How to remove all element from Hashset.

How to remove all element from Hashset.

HashSet is set type Collection. It supports only unique value. We can not store duplicate value in  it .
In this example, you will see the use of clear method of HashSet. It is used to remove all element of hashset.

Code

HashSetRemoveElement.java

package net.roseindia;

import java.util.HashSet;

public class RemoveAllElement {

public static void main(String[] arg) {

HashSet obHashSet = new HashSet();

obHashSet.add(new Integer(1));

obHashSet.add(new Integer(2));

obHashSet.add(new Integer(3));

obHashSet.add(new Integer(4));

System.out.println("Element before clear method : " + obHashSet);

obHashSet.clear();

System.out.println("Check HashSet : " + obHashSet.isEmpty());

System.out.println("Element after clear method : " + obHashSet);

}

}

Output :

Element before clear method : [1, 2, 3, 4]

Check HashSet : true

Element after clear method : []

Advertisement

Related Tags for How to remove all element from Hashset.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 4, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com