Click here to watch in Youtube :
https://www.youtube.com/watch?v=0N9mOZtGNa0&list=UUhwKlOVR041tngjerWxVccw
DescendingCountryCodeComparator.java
TreeMapExample.java
https://www.youtube.com/watch?v=0N9mOZtGNa0&list=UUhwKlOVR041tngjerWxVccw
DescendingCountryCodeComparator.java
import java.util.Comparator; public class DescendingCountryCodeComparator implements Comparator<String> { /* * This method is used to arrange the CountryCodes in descending order. */ @Override public int compare( String countryCode1, String countryCode2 ) { System.out .println("\nCompare method has been called in DescendingCountryCodeComparator,
\nto arrange"+ " the CountryCodes in decending order : "); System.out.println("countryCode1 = " + countryCode1 + ", countryCode2 = " + countryCode2 + "\n"); return countryCode2.compareTo(countryCode1); } }
import java.util.TreeMap; /* * Example of comparator() method. */ public class TreeMapExample { public static void main( String[] args ) { DescendingCountryCodeComparator descendingCountryCodeComparator
= new DescendingCountryCodeComparator(); TreeMap<String, String> treeMap = new TreeMap<String, String>( descendingCountryCodeComparator); /* * Key is CountryCode - Value is CountryName */ System.out.println("Key:CN,Value:CHINA is going to be add in treeMap"); treeMap.put("CN", "CHINA"); System.out.println("Key:BE,Value:BELGIUM is going to be add in treeMap"); treeMap.put("BE", "BELGIUM"); System.out.println("Key:AF,Value:AFGHANISTAN is going to be add in treeMap"); treeMap.put("AF", "AFGHANISTAN"); System.out.println("Key:DK,Value:DENMARK is going to be add in treeMap"); treeMap.put("DK", "DENMARK"); System.out.println("treeMap : " + treeMap + "\n"); } }
Key:CN,Value:CHINA is going to be add in treeMap Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = CN, countryCode2 = CN Key:BE,Value:BELGIUM is going to be add in treeMap Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = BE, countryCode2 = CN Key:AF,Value:AFGHANISTAN is going to be add in treeMap Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = AF, countryCode2 = CN Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = AF, countryCode2 = BE Key:DK,Value:DENMARK is going to be add in treeMap Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = DK, countryCode2 = BE Compare method has been called in DescendingCountryCodeComparator, to arrange the CountryCodes in decending order : countryCode1 = DK, countryCode2 = CN treeMap : {DK=DENMARK, CN=CHINA, BE=BELGIUM, AF=AFGHANISTAN}
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoDescOrderCountryCode.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment