TreeSet Example in Java

By | December 26, 2010
TreeSet Example in Java Collection Framework. This Java TreeSet example describes the basic operations performed on the TreeSet in Collection Framework.
TreeSet Example in Java Collection Framework. This Java TreeSet example describes the basic operations performed on the TreeSet in Collection Framework.

Program:

import java.util.TreeSet;
import java.util.Iterator;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet ts = new TreeSet();
ts.add(“A”);
ts.add(“E”);
ts.add(“C”);
ts.add(“B”);
ts.add(“D”);
System.out.println(“TreeSet: “+ts);
Iterator it = ts.iterator();
System.out.print(“TreeSet-element: “);
while(it.hasNext())
System.out.print(” “+it.next());
}
}

Result:

TreeSet: [A, B, C, D, E]
TreeSet-element:  A B C D E

Leave a Reply

Your email address will not be published. Required fields are marked *