Tuesday 13 January 2015

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


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

AscendingAlphabetsComparator.java
import java.util.Comparator;

public class AscendingAlphabetsComparator implements Comparator<String>
{

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

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

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

        return alphabet1.compareTo(alphabet2);
    }

}
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.
         */
        AscendingAlphabetsComparator ascendingAlphabetsComparator = new AscendingAlphabetsComparator();
        
        TreeSet<String> treeSet = new TreeSet<String>(
                ascendingAlphabetsComparator);

        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 AscendingAlphabetsComparator, to arrange the Alphabets in ascending order : alphabet1 = B, alphabet2 = B

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

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

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

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

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