Click here to watch in Youtube :
https://www.youtube.com/watch?v=hBaoCkE5Zks&list=UUhwKlOVR041tngjerWxVccw
AscendingEmployeeIdComparator.java
TreeMapExample.java
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; } } }
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"); } }
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}
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoAscOrderEmpId.zip?attredirects=0&d=1
See also:
salesforce online course
ReplyDeletehadoop online course
Data Science online course
linux online course
etl testing online course
web methods online course