Friday 27 February 2015

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


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

AscendingEmployeeIdComparator.java
import java.util.Comparator;

public class AscendingEmployeeIdComparator implements Comparator<Integer>
{

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

        System.out
                .print("Compare method has been called in AscendingEmployeeIdComparator,\nto arrange"
                        + " the EmpId's in ascending 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 )
    {

        AscendingEmployeeIdComparator ascendingEmployeeIdComparator 
                                                 = new AscendingEmployeeIdComparator();

        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(
                ascendingEmployeeIdComparator);
        /*
         * 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 AscendingEmployeeIdComparator,
to arrange the EmpId's in ascending order : employeeId1 = 20, employeeId2 = 20

Key:10,Value:Ajay is going to be add in treeMap.
Compare method has been called in AscendingEmployeeIdComparator,
to arrange the EmpId's in ascending order : employeeId1 = 10, employeeId2 = 20

Key:40,Value:Carol is going to be add in treeMap.
Compare method has been called in AscendingEmployeeIdComparator,
to arrange the EmpId's in ascending order : employeeId1 = 40, employeeId2 = 20

Key:30,Value:Dave is going to be add in treeMap.
Compare method has been called in AscendingEmployeeIdComparator,
to arrange the EmpId's in ascending order : employeeId1 = 30, employeeId2 = 20

Compare method has been called in AscendingEmployeeIdComparator,
to arrange the EmpId's in ascending order : employeeId1 = 30, employeeId2 = 40

treeMap : {10=Ajay, 20=Balu, 30=Dave, 40=Carol}
To Download TreeMapDemoAscOrderEmpId Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoAscOrderEmpId.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • 1 comment: