Java – Display a list of countries using Locale.getISOCountries()
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.memorynotfound.util.locale;
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(Country o) {
return this.name.compareTo(o.getName());
}
@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 the Locale.getISOCountries()
. This returns a list of all 2-letter country codes defined in ISO-3166
.
Note: TheLocale
class also supports other codes for country (region), such as 3-letter numericUN M.49
area codes. Therefore, the list returned by this method does not contain ALL valid codes that can be used to createLocales
.
package com.memorynotfound.util.locale;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class ListCountries {
public static void main(String... args){
// 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");
}
}
Output
Country{iso='AFG', code='AF', name='Afghanistan'}
Country{iso='ALB', code='AL', name='Albania'}
Country{iso='DZA', code='DZ', name='Algeria'}
Country{iso='ASM', code='AS', name='American Samoa'}
Country{iso='AND', code='AD', name='Andorra'}
//... other countries emitted.
found: 250 countries
Display a list of countries in Pre Defined Language
We can also print the countries in a pre defined language. In this example we display all the countries in the Locale.JAPAN
.
package com.memorynotfound.util.locale;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class ListCountriesDefinedLanguage {
public static void main(String... args){
// 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.JAPAN);
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");
}
}
Output
Country{iso='ISL', code='IS', name='アイスランド'}
Country{iso='IRL', code='IE', name='アイルランド'}
Country{iso='AZE', code='AZ', name='アゼルバイジャン'}
Country{iso='AFG', code='AF', name='アフガニスタン'}
Country{iso='ASM', code='AS', name='アメリカンサモア'}
//... other countries emitted
found: 250 countries
Display a list of countries in their own native language
To display a list of countries in their own native language is a bit trickier. Not all locales are supported by default. But to get an idea of how you can obtain a list of countries in their own language, we wrote the following example.
package com.memorynotfound.util.locale;
import java.util.*;
import java.util.stream.Collectors;
public class ListCountriesOwnLanguage {
public static void main(String... args){
// 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");
}
}
Output
could not find resource for: AD mapping default lang
could not find resource for: AF mapping default lang
could not find resource for: AG mapping default lang
could not find resource for: AI mapping default lang
could not find resource for: AM mapping default lang
//... output emitted
Country{iso='AFG', code='AF', name='Afghanistan'}
Country{iso='CZE', code='CZ', name='Česká republika'}
Country{iso='CYP', code='CY', name='Κύπρος'}
Country{iso='ISR', code='IL', name='ישראל'}
Country{iso='JOR', code='JO', name='الأردن'}
//... other countries emitted
available locales: 160
found: 250 countries