Share on Google+Share on Google+

Example of Hashset iterator method in java.

Example of Hashset iterator method in java.

Example of Hashset iterator method in java.

In this exmple of HashSet class,  you will see the use of iterator() method. It is used for traversing all element from HashSet.


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


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 : []



Posted on: April 4, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.