Monday 5 January 2015

Java : Collection Framework : TreeSet (descendingSet)


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

TreeSetExample.java
import java.util.NavigableSet;
import java.util.TreeSet;

/*
 * Example of descendingSet() method.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {

        TreeSet<Integer> treeSet = new TreeSet<Integer>();

        treeSet.add(40);
        treeSet.add(20);
        treeSet.add(30);
        treeSet.add(10);
        treeSet.add(50);

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

        /*
         * Returns a reverse order view of the elements contained in this set.
         * The descending set is backed by this set, so changes to the set are
         * reflected in the descending set, and vice-versa.
         * 
         * If either set is modified while an iteration over either set is in
         * progress (except through the iterator's own remove operation), the
         * results of the iteration are undefined.
         */

        NavigableSet<Integer> navigableSet = treeSet.descendingSet();

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

        for( Integer value : navigableSet )
        {
            System.out.println(value);
        }

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

navigableSet : [50, 40, 30, 20, 10]

50
40
30
20
10
To Download TreeSetDemoDescendingSet Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoDescendingSet.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