In the following tutorial we demonstrate how to Display a list of countries using Locale.getISOCountries()
. First we show how to obtain the country list in a pre defined language. The second example shows how to obtain the country list in their own native language.
Mapping Locale to Country Object
For this example we’ll use a custom Country object to map the values from the Locale object.
package com.sheting.basic.utilities;
public class Country implements Comparable {
private String iso;
private String code;
private String name;
public Country(String iso, String code, String name) {
this.iso = iso;
this.code = code;
this.name = name;
}
public String getIso() {
return iso;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
@Override
public int compareTo(Object o) {
Country country = (Country) o;
return this.iso.compareTo(country.getIso());
}
@Override
public String toString() {
return "Country{" + "iso='" + iso + '\'' + ", code='" + code + '\'' + ", name='" + name + '\'' + '}';
}
}
Display a list of countries
In the first example we we retrieve all available ISO Countries using theLocale.getISOCountries()
. This returns a list of all 2-letter country codes defined in ISO-3166
.
Note: The Locale
class also supports other codes for country (region), such as 3-letter numeric UN M.49
area codes. Therefore, the list returned by this method does not contain ALL valid codes that can be used to create Locales
.
package com.sheting.basic.utilities;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
public class ListCountries {
public static void main(String... args) {
listCountries();
listCountriesDefinedLanguage();
listCountriesOwnLanguage();
}
// Display a list of countries
public static void listCountries() {
// Create a collection of all available countries
List<Country> countries = new ArrayList<Country>();
// Map ISO countries to custom country object
String[] countryCodes = Locale.getISOCountries();
for (String countryCode : countryCodes) {
Locale locale = new Locale("", countryCode);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
countries.add(new Country(iso, code, name));
}
// Sort countries
Collections.sort(countries);
// Loop over collection of countries and print to console
countries.forEach((System.out::println));
// print total number of countries
System.out.println("found: " + countries.size() + " countries");
}
// Display a list of countries in Pre Defined Language
public static void listCountriesDefinedLanguage() {
// Create a collection of all available countries
List<Country> countries = new ArrayList<Country>();
// Map ISO countries to custom country object
String[] countryCodes = Locale.getISOCountries();
for (String countryCode : countryCodes) {
Locale locale = new Locale("", countryCode);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry(Locale.ENGLISH);
countries.add(new Country(iso, code, name));
}
// Sort countries
Collections.sort(countries);
// Loop over collection of countries and print to console
countries.forEach(System.out::println);
// print total number of countries
System.out.println("found: " + countries.size() + " countries");
}
// Display a list of countries in their own native language
public static void listCountriesOwnLanguage() {
// Get all available locales
List<Locale> availableLocales = Arrays.asList(Locale.getAvailableLocales());
// Get all available ISO countries
String[] countryCodes = Locale.getISOCountries();
// Create a collection of all available countries
List<Country> countries = new ArrayList<Country>();
// Map ISO countries to custom country object
for (String countryCode : countryCodes) {
Optional<Locale> candidate = availableLocales.stream().filter(l -> l.getCountry().equals(countryCode))
.collect(Collectors.reducing((a, b) -> null));
Locale locale;
if (candidate.isPresent()) {
locale = candidate.get();
} else {
// System.out.println("could not find resource for: " + countryCode + " mapping
// default lang");
locale = new Locale("", countryCode);
}
String iso = locale.getISO3Country();
String code = locale.getCountry();
String country = locale.getDisplayCountry(locale);
countries.add(new Country(iso, code, country));
}
// Sort countries
Collections.sort(countries);
// Loop over collection of countries and print to console
countries.forEach((System.out::println));
// Print out available locales
System.out.println("available locales: " + availableLocales.size());
// print total number of countries
System.out.println("found: " + countries.size() + " countries");
}
}