Iterator<E>
iterator()
Returns an iterator over the
elements in this set, in ascending order.
import java.util.*; class TreeSetIterator{ public static void main(String args[]){ TreeSet<Integer> mySet = new TreeSet<> (); /* Add Elements too mySet */ for(int i=20; i>0; i-=2){ mySet.add(i); } System.out.println("Elements in mySet are"); System.out.println(mySet+"\n"); Iterator<Integer> iter1 = mySet.iterator(); System.out.println("Printing Elements using iterator"); while(iter1.hasNext()){ System.out.print(iter1.next() + " "); } } }
Output
Elements in mySet are [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Printing Elements using iterator 2 4 6 8 10 12 14 16 18 20
No comments:
Post a Comment