Display a list of countries in Java

In this article, we show you how to use the Locale class to play around the list of countries.

P.S Tested with JDK 1.6

1. List of Countries

The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166.

ListCountry.java
package com.webmitta.model;
import java.util.Locale;
public class ListCountry {
    public static void main(String[] args) {
	ListCountry obj = new ListCountry();
	obj.run();
    public void run() {
	String[] locales = Locale.getISOCountries();
	for (String countryCode : locales) {
		Locale obj = new Locale("", countryCode);
		System.out.println("Country Code = " + obj.getCountry() 
			+ ", Country Name = " + obj.getDisplayCountry());
	System.out.println("Done");

Output

Country Code = AD, Country Name = Andorra
Country Code = AE, Country Name = United Arab Emirates
Country Code = AF, Country Name = Afghanistan
Country Code = AG, Country Name = Antigua and Barbuda
Country Code = AI, Country Name = Anguilla
Country Code = AL, Country Name = Albania
//skip ...

2. List of Countries in defined Languages

Get a list of countries, and display the country name in Chinese.

ListCountry.java
package com.webmitta.model;
import java.util.Locale;
public class ListCountry {
    public static void main(String[] args) {
	ListCountry obj = new ListCountry();
	//get a list of countries and display in Chinese
	obj.getListOfCountries(Locale.Chinese);
	//display in frence
	//obj.getListOfCountries(Locale.FRENCH);
    public void getListOfCountries(Locale locale) {
	String[] locales = Locale.getISOCountries();
	for (String countryCode : locales) {
	    Locale obj = new Locale("", countryCode);
	    System.out.println("Country Code = " + obj.getCountry() 
		+ ", Country Name = " + obj.getDisplayCountry(locale));

Output, Eclipse with UTF-8 output enabled

3. List of Countries in own Languages

Get a list of countries, and display the country name in own country’s languages. The trick is creating a Map object and store the country code and country languages, for query later.

Note
Beware, not all countries have own languages.
ListCountry.java
package com.webmitta.model;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
public class ListCountry {
    private Map<String, String> languagesMap = new TreeMap<String, String>();
    public ListCountry() {
	initLanguageMap();
    public static void main(String[] args) {
	ListCountry obj = new ListCountry();
	obj.getListOfCountries();
    public void getListOfCountries() {
	String[] countries = Locale.getISOCountries();
	int supportedLocale = 0, nonSupportedLocale = 0;
	for (String countryCode : countries) {
	  Locale obj = null;
	  if (languagesMap.get(countryCode) == null) {
		obj = new Locale("", countryCode);
		nonSupportedLocale++;
	  } else {
		//create a Locale with own country's languages
		obj = new Locale(languagesMap.get(countryCode), countryCode);
		supportedLocale++;
	  System.out.println("Country Code = " + obj.getCountry() 
		+ ", Country Name = " + obj.getDisplayCountry(obj)
		+ ", Languages = " + obj.getDisplayLanguage());
	  System.out.println("nonSupportedLocale : " + nonSupportedLocale);
	  System.out.println("supportedLocale : " + supportedLocale);
    // create Map with country code and languages
    public void initLanguageMap() {
	Locale[] locales = Locale.getAvailableLocales();
	for (Locale obj : locales) {
	  if ((obj.getDisplayCountry() != null) && (!"".equals(obj.getDisplayCountry()))) {
		languagesMap.put(obj.getCountry(), obj.getLanguage());

Output, Eclipse with UTF-8 output enabled

References

  1. Country Codes – ISO 3166
  2. ISO 3166-1 decoding table
  3. Java tutorial : Creating a Locale
  4. Locale JavaDoc
  5. Getting Countries list in Java
  6. How To Display Chinese Character In Eclipse Console

上一篇: How to read and parse CSV file in Java
下一篇: MongoDB import and export example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 

一号门极客CMS,由Python,Django,MySQL,Apache,Mod Wsgi 强力驱动.
鄂ICP备14001754号-3