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.
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
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:
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!!
i solved it… it was my mistake.
thanks.
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.
Kosovo is not country, but part of Serbia.