Using Tree Set in Java



TreeSet has the following constructors:
TreeSet( ) --constructs an empty tree set  sorted in ascending order
TreeSet(Collection<? extends E> c)--builds a tree set that contains the elements of c
TreeSet(Comparator<? super E> comp)--constructs an empty tree set that will be sorted according to the comparator specified by comp.
TreeSet(SortedSet<E>ss)--a tree set that contains the elements of ss

In TreeSet elements are arranged automatically in sorted order.
// Comment
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
packagecom.allinalljava;
importjava.util.*;
/**
 *
 * @author allinalljava
 */
public class JavaTreeSet {
    // Demonstrate TreeSet.


public static void main(String args[]) {
// Create a tree set.
TreeSettreeSet = new TreeSet();
// Add elements to the tree set.
treeSet.add("C");
treeSet.add("A");
treeSet.add("B");
treeSet.add("E");
treeSet.add("F");
treeSet.add("D");
System.out.println("TreeSetContail Elements"+treeSet);
}
}


Output 
 TreeSetContailElements[A, B, C, D, E, F]
raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...