The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : TreeMap (Comparator - Asc Order CountryCode)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : TreeMap (Comparator - Asc Order CountryCode) Posted: Feb 27, 2015 11:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : TreeMap (Comparator - Asc Order CountryCode)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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

AscendingCountryCodeComparator.java
import java.util.Comparator;

public class AscendingCountryCodeComparator implements Comparator<String>
{

/*
* This method is used to arrange the CountryCodes in Ascending order.
*/

@Override
public int compare( String countryCode1, String countryCode2 )
{

System.out
.println("\nCompare method has been called in AscendingCountryCodeComparator,\nto arrange"
+ " the CountryCodes in ascending order : ");

System.out.println("countryCode1 = " + countryCode1
+ ", countryCode2 = " + countryCode2 + "\n");

return countryCode1.compareTo(countryCode2);
}

}
TreeMapExample.java
import java.util.TreeMap;

/*
* Example of comparator() method.
*/

public class TreeMapExample
{
public static void main( String[] args )
{

AscendingCountryCodeComparator ascendingCountryCodeComparator = new AscendingCountryCodeComparator();

TreeMap<String, String> treeMap = new TreeMap<String, String>(
ascendingCountryCodeComparator);
/*
* 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 AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = CN, countryCode2 = CN

Key:BE,Value:BELGIUM is going to be add in treeMap

Compare method has been called in AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = BE, countryCode2 = CN

Key:AF,Value:AFGHANISTAN is going to be add in treeMap

Compare method has been called in AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = AF, countryCode2 = CN


Compare method has been called in AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = AF, countryCode2 = BE

Key:DK,Value:DENMARK is going to be add in treeMap

Compare method has been called in AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = DK, countryCode2 = BE


Compare method has been called in AscendingCountryCodeComparator,
to arrange the CountryCodes in ascending order :
countryCode1 = DK, countryCode2 = CN

treeMap : {AF=AFGHANISTAN, BE=BELGIUM, CN=CHINA, DK=DENMARK}
To Download TreeMapDemoAscOrderCountryCode Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeMapDemoAscOrderCountryCode.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Read: Java : Collection Framework : TreeMap (Comparator - Asc Order CountryCode)

    Topic: Async abstractions using rx-java Previous Topic   Next Topic Topic: Gradle “Hello World” Tutorial

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use