Question

Using Java, you can get the list of ISO2 codes through Locale.getISOCountries() (see this related question Best way to get a list of countries in Java).

However, I would like to have the list of all country names (in English for example) and not the list of ISO2 country codes. How can I do that by programming in Java or Groovy ?

Thank you very much,

Fabien.

Was it helpful?

Solution

Using Groovy, this prints a sorted list of country names:

def countries = [] as SortedSet

Locale.availableLocales*.displayCountry.each {
  if (it) {
    countries << it
  }
}

println countries

In my locale, this prints

[Albania, Algeria, Argentina, Australia, Austria, Bahrain, ..., Yemen]

You need to use a Set rather than a List because there are multiple locales for some countries, e.g. French Canada and English Canada locales for the country Canada.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top