Javaの国のリストを表示する

この記事では、Localeクラスを使用して国のリストを操作する方法を示します。
P.S Tested with JDK 1.6
1. 国のリスト
Locale.getISOCountries()は、ISO 3166で定義されているすべての2文字の国コードのリストを返します。
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");
}
}
出力
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. 定義された言語の国のリスト
国のリストを取得し、国名を中国語で表示します。
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));
}
}
}
出力、UTF-8出力を有効にしたEclipse

3. 自国語の国のリスト
国のリストを取得し、国名を自国の言語で表示します。 秘訣は、Mapオブジェクトを作成し、後でクエリを実行するために国コードと国の言語を保存することです。
Note
すべての国が独自の言語を持っているわけではないことに注意してください。
ListCountry.java
package com.webmitta.model;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
public class ListCountry {
private Map languagesMap = new TreeMap();
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());
}
}
}
}
出力、UTF-8出力を有効にしたEclipse
