Tuesday 13 January 2015

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


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

AscendingNumberComparator.java
import java.util.Comparator;

public class AscendingNumberComparator implements Comparator<Integer>
{

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

        System.out
                .print("Compare method has been called in AscendingNumberComparator, to arrange"
                        + " the numbers in ascending 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.
         */
        AscendingNumberComparator ascendingNumberComparator = new AscendingNumberComparator();
        
        TreeSet<Integer> treeSet = new TreeSet<Integer>(
                ascendingNumberComparator);

        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 AscendingNumberComparator, to arrange the numbers in ascending order : number1 = 20, number2 = 20

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

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

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

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

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

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