Problem : Create a dynamic Set contains unique elements in sorted order.
Program :Step 1 - CodingCreate a text file c:\sunilos\TestTreeSet.java and copy below contents.import java.util.*; /* * A program to implement TreeSet. */ public class TestTreeSet { public static void main(String[] args) { TreeSet tSet = new TreeSet(); // You can insert any object in the TreeSet. Here it is string tSet.add("Jay"); tSet.add("Viru"); tSet.add("Basanti"); /* * You can insert any object in the TreeSet. Primitive data type need to * be converted into Objects before inserting. * */ // Get all elements and print with help of Iterator interface Iterator it = tSet.iterator(); // Get an iterator System.out.println("Print All Elements with help of Iterator "); while (it.hasNext()) { // Checks if any element in list Object oo = it.next(); // Get next available element System.out.println(" From Iterator -- " + oo); } Object obj = tSet.last();//Show Last record System.out.println("Last value in TreeSet : " + obj); //Show the size of TreeSet System.out.println("Size of TreeSet : " + tSet.size()); tSet.clear();//Clear all the records //Show that TreeSet is empty or not System.out.println("Is Empty " + tSet.isEmpty()); } } Step 2 - Deployment
Step 3 - Testing
OutputC:\sunilos>java TestTreeSetPrint All Elements with help of Iterator From Iterator -- Basanti From Iterator -- Jay From Iterator -- Viru Last value in TreeSet : Viru Size of TreeSet : 3 Is Empty true FAQWhat is TreeSet?
Is TreeSet thread-safe?No, it is not synchronized. If multiple threads access a set concurrently, then it must be synchronized externally with help ofSortedSet s = Collections.synchronizedSortedSet(new TreeSet()); Most Interesting methods in TreeSet?
<<Previous | Next>> |