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.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");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值