itrator in Java TreeSet Example

itrator in Java TreeSet Example


Posted in : Java Posted on : November 24, 2010 at 5:32 PM Comments : [ 0 ]

This Java Example shows how to get the elements exists in TreeSet.

itrator in Java TreeSet Example

This Java Example shows how to get the  elements exists in TreeSet. To get the elements exists in TreeSet We use int itrator() method of TreeSet class.  It returns an Iterator object for the collection which may be used to retrieve an TreeSet object.

Example:- ItrateThroughElement.java

package devmanuals.com;

import java.util.*;

public class ItrateThroughElement {
	public static void main(String[] args) {

		TreeSet st = new TreeSet();

		st.add("Gyan");
		st.add("Rohit");
		st.add("Anand");
		st.add("Arunesh");

		Iterator itr = st.iterator();
       System.out.println("The element of the TreeSet is :");
		while (itr.hasNext()) {
			String str = (String) itr.next();
			System.out.println("Name :" + str);
		}
	}
}

Output:-

The element of the TreeSet is : 

Name :Anand 

Name :Arunesh 

Name :Gyan 

Name :Rohit

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics