Tuesday 13 January 2015

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


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

DescendingAlphabetsComparator.java
import java.util.Comparator;

public class DescendingAlphabetsComparator implements Comparator<String>
{

    /*
     * This method used to arrange the Alphabets in descending order.
     */
    @Override
    public int compare( String alphabet1, String alphabet2 )
    {

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

        System.out
                .println("alphabet1 = " + alphabet1 + ", alphabet2 = " + alphabet2 + "\n");

        return alphabet2.compareTo(alphabet1);
    }

}
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.
         */
        DescendingAlphabetsComparator descendingAlphabetsComparator = 
                                              new DescendingAlphabetsComparator();

        TreeSet<String> treeSet = new TreeSet<String>(
                descendingAlphabetsComparator);

        System.out.println("B" + " is going to be add in treeSet");
        treeSet.add("B");
        System.out.println("C" + " is going to be add in treeSet");
        treeSet.add("C");
        System.out.println("A" + " is going to be add in treeSet");
        treeSet.add("A");
        System.out.println("B" + " is going to be add in treeSet");
        treeSet.add("D");

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

    }
}
Output
B is going to be add in treeSet
Compare method has been called in DescendingAlphabetsComparator, to arrange the Alphabets in descending order : alphabet1 = B, alphabet2 = B

C is going to be add in treeSet
Compare method has been called in DescendingAlphabetsComparator, to arrange the Alphabets in descending order : alphabet1 = C, alphabet2 = B

A is going to be add in treeSet
Compare method has been called in DescendingAlphabetsComparator, to arrange the Alphabets in descending order : alphabet1 = A, alphabet2 = B

B is going to be add in treeSet
Compare method has been called in DescendingAlphabetsComparator, to arrange the Alphabets in descending order : alphabet1 = D, alphabet2 = B

Compare method has been called in DescendingAlphabetsComparator, to arrange the Alphabets in descending order : alphabet1 = D, alphabet2 = C

treeSet : [D, C, B, A]
To Download TreeSetDemoAlphabetsDescComparator Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoAlphabetsDescComparator.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