3:59 pm - Saturday February 25, 2017

Iterate all elements in Hashset using Iterator()

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 Iterate all elements in Hashset using Iterator() method.

Returns an iterator over the elements in this set.

 Iteration will be happen if the element is available at next  level so on.

Example

    import java.util.HashSet;

    import java.util.Iterator;     

    public class IteratorElements

  {   

     public static void main(String[] args)

     {                    

            HashSet<String> hashSet = new HashSet<String>();

            hashSet.add("element_1");

            hashSet.add("element_2");

            hashSet.add("element_3");               

            Iterator<String> it = hashSet.iterator();     

            System.out.println("Elements in HashSet :");

            while(it.hasNext())   

            System.out.println(it.next());

      }

    }

Output

         element_1

         element_2

         element_3

BACK

Filed in: Java Examples

Comments are closed.