Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎

TestTreeSet

Problem : Create a dynamic Set contains unique elements in sorted order.

Program :

Step 1 - Coding

Create 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

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestTreeSet.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestTreeSet.java with help of 
    • javac TestTreeSet.java command. Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestTreeSet command.

Output

C:\sunilos>java TestTreeSet
Print 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


FAQ

What is TreeSet?

  1. This class implements the Set interface, backed by a TreeMap instance.
  2. This class guarantees that the sorted set will be in ascending element order. 

Is TreeSet thread-safe?

No, it is not synchronized. If multiple threads access a set concurrently, then it must be synchronized externally with help of
     SortedSet s = Collections.synchronizedSortedSet(new TreeSet());

Most Interesting methods in TreeSet?

  • add() - Adds the specified element to this set if it is not already present.
  • clear() - Removes all of the elements from this set.
  • first() - Returns the first (lowest) element currently in this sorted set.
  • isEmpty() - Returns true if this set contains no elements.
  • iterator() - Returns an iterator over the elements in this set.
  • last() - Returns the last (highest) element currently in this sorted set.
  • remove() - Removes the specified element from this set if it is present.
  • size() - Returns the number of elements in this set (its cardinality).


<<Previous | Next>>
ċ
TestTreeSet.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:24 PM
Comments