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 |
[ 0 ] Comments