Friday 27 February 2015

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


Click here to watch in Youtube :
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);
    }

}
TreeMapExample.java
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");

    }
}
Output
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}
To Download TreeMapDemoDescOrderCountryCode Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoDescOrderCountryCode.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