2:18 am - Tuesday October 24, 2017

Iterate Treeset elements using Iterator 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 Treeset elements using  Iterator<E> iterator() method.

To read objects using Iterator.

By calling iterator() method you will get Iterator object,

Through which you can iterate through all the elements of the TreeSet.

Example

import java.util.Iterator;

import java.util.TreeSet;

public class MySetIteration

{

   public static void main(String a[])

{     

       TreeSet<String> ts = new TreeSet<String>();

       ts.add("one");

       ts.add("two");

       ts.add("three");

       Iterator<String> itr = ts.iterator();

       while(itr.hasNext()){

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

       }

   }

}

Output

         one

         three

         two

 

BACK

Filed in: Java Examples

Comments are closed.