Main Tutorials

Display a list of countries in Java

world-country

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

country list

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

country list

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Omar
1 year ago

hey i have a question how would i create Your task is to create a data type for a country. bjects of the Country class have the following properties:

  • name;
  • population;
  • an area; and
  • gdp (Gross Domestic Product).
aashish
10 years ago

nice…. !! i am trying to print the count of with country in 1 example….. but i stuck somewhere can u please help me to count this!!

aashish
10 years ago
Reply to  aashish

i solved it… it was my mistake.

kon
10 years ago

thanks.

Anurag
7 years ago

Locale will not give you full list of country code.
like XK – Kosovo.
Please tell me any other way to get list of all country code.

GDR
6 years ago
Reply to  Anurag

Kosovo is not country, but part of Serbia.