Friday 27 February 2015

Java : Collection Framework : TreeMap (Comparator - Desc Order EmployeeId)


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

DescendingEmpIoyeeIdComparator.java
import java.util.Comparator;

public class DescendingEmpIoyeeIdComparator implements Comparator<Integer>
{

    /*
     * This method used to arrange the employeeId's in descending order.
     */
    @Override
    public int compare( Integer employeeId1, Integer employeeId2 )
    {

        System.out
                .println("\nCompare method has been called in 
                   DescendingEmpIoyeeIdComparator, \nto arrange"
                        + " the employeeId's in descending order : ");

        System.out
                .println("employeeId1 = " + employeeId1 + ", employeeId2 = " + employeeId2 + "\n");

        if( employeeId1 < employeeId2 )
        {
            
            return 1;
        }
        else
        {
            return -1;
        }
    }

}
TreeMapExample.java
import java.util.TreeMap;

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

        DescendingEmpIoyeeIdComparator descendingEmpIoyeeIdComparator 
                                                   = new DescendingEmpIoyeeIdComparator();

        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(
                descendingEmpIoyeeIdComparator);
        /*
         * Key is EmployeeId - Value is EmployeeName
         */

        System.out.println("Key:20,Value:Balu is going to be add in treeMap");
        treeMap.put(20, "Balu");

        System.out.println("Key:10,Value:Ajay is going to be add in treeMap");
        treeMap.put(10, "Ajay");

        System.out.println("Key:40,Value:Carol is going to be add in treeMap");
        treeMap.put(40, "Carol");

        System.out.println("Key:30,Value:Dave is going to be add in treeMap");
        treeMap.put(30, "Dave");

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

    }
}
Output
Key:20,Value:Balu is going to be add in treeMap

Compare method has been called in DescendingEmpIoyeeIdComparator, 
to arrange the employeeId's in descending order : 
employeeId1 = 20, employeeId2 = 20

Key:10,Value:Ajay is going to be add in treeMap

Compare method has been called in DescendingEmpIoyeeIdComparator, 
to arrange the employeeId's in descending order : 
employeeId1 = 10, employeeId2 = 20

Key:40,Value:Carol is going to be add in treeMap

Compare method has been called in DescendingEmpIoyeeIdComparator, 
to arrange the employeeId's in descending order : 
employeeId1 = 40, employeeId2 = 20

Key:30,Value:Dave is going to be add in treeMap

Compare method has been called in DescendingEmpIoyeeIdComparator, 
to arrange the employeeId's in descending order : 
employeeId1 = 30, employeeId2 = 20


Compare method has been called in DescendingEmpIoyeeIdComparator, 
to arrange the employeeId's in descending order : 
employeeId1 = 30, employeeId2 = 40

treeMap : {40=Carol, 30=Dave, 20=Balu, 10=Ajay}
To Download TreeMapDemoDescOrderEmpId Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoDescOrderEmpId.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