開発者ドキュメント

Javaの国リストを表示する


world-country、width = 500、height = 262

この記事では、 `Locale`クラスを使って国のリストを操作する方法を説明します。


P.S JDK 1.6

でテスト済み

1.国リスト

`Locale.getISOCountries()`は、http://www.iso.org/iso/country__codes[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`オブジェクトを作成し、国コードと国の言語を保存することです。

ListCountry.java

package com.webmitta.model;

import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

public class ListCountry {

    private Map<String, String> languagesMap = new TreeMap<String, String>();

    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



参考文献

チュートリアル:ロケールの作成]。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Locale.html

[Locale

Javaの国リスト]。 link://java/how-to-display-chinese-character-in-eclipse-console/[どうやって

Eclipseコンソールで漢字を表示する]

リンク://タグ/java/[java]リンク://タグ/ロケール/[ロケール]

モバイルバージョンを終了