Tuesday 30 December 2014

Java : Collection Framework : TreeSet (Constructor Accepts Collection)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=46Us9a1EGe8&list=UUhwKlOVR041tngjerWxVccw

TreeSetExample.java
import java.util.LinkedList;
import java.util.TreeSet;

/*
 * Example of TreeSet(Collection<? extends E> c) constructor.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(50);
        linkedList.add(30);
        linkedList.add(40);
        linkedList.add(10);
        linkedList.add(20);

        System.out.println("linkedList : " + linkedList + "\n");
        
        /*
         * Constructs a new tree set containing the elements in the specified
         * collection, sorted according to the natural ordering of its elements.
         * 
         * All elements inserted into the set must implement the Comparable
         * interface.
         */
        TreeSet<Integer> treeSet = new TreeSet<Integer>(linkedList);

        System.out.println("treeSet : " + treeSet + "\n");

    }
}
Output
linkedList : [50, 30, 40, 10, 20]

treeSet : [10, 20, 30, 40, 50]
To Download TreeSetDemoConstAcceptsCollection Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoConstAcceptsCollection.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment