Tuesday 13 January 2015

Java : Collection Framework : TreeSet (Arrange the numbers in descending order using Comparator)


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

DescendingNumberComparator.java
import java.util.Comparator;

public class DescendingNumberComparator implements Comparator<Integer>
{

    /*
     * This method used to arrange the numbers in descending order.
     */
    @Override
    public int compare( Integer number1, Integer number2 )
    {

        System.out
                .print("Compare method has been called in DescendingNumberComparator, to arrange"
                        + " the numbers in descending order : ");

        System.out
                .println("number1 = " + number1 + ", number2 = " + number2 + "\n");

        if( number1 < number2 )
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }

}
TreeSetExample.java
import java.util.TreeSet;

/*
 * Example of TreeSet(Comparator<? super E> comparator) Constructor.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {

        /*
         * Constructs a new, empty tree set, sorted according to the specified
         * comparator.
         *
         * All elements inserted into the set must be mutually comparable by the
         * specified comparator: comparator.compare(e1, e2) must not throw a
         * ClassCastException for any elements e1 and e2 in the set.
         *
         * If the user attempts to add an element to the set that violates this
         * constraint, the add call will throw a ClassCastException.
         */

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

        System.out.println(20 + " is going to be add in treeSet");
        treeSet.add(20);
        System.out.println(40 + " is going to be add in treeSet");
        treeSet.add(40);
        System.out.println(30 + " is going to be add in treeSet");
        treeSet.add(30);
        System.out.println(10 + " is going to be add in treeSet");
        treeSet.add(10);

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

    }
}
Output
20 is going to be add in treeSet
Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 20, number2 = 20

40 is going to be add in treeSet
Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 40, number2 = 20

30 is going to be add in treeSet
Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 30, number2 = 20

Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 30, number2 = 40

10 is going to be add in treeSet
Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 10, number2 = 30

Compare method has been called in DescendingNumberComparator, to arrange the numbers in descending order : number1 = 10, number2 = 20

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