Is there an open source java enum of ISO 3166-1 country codes

前端 未结 11 2635
南笙
南笙 2020-12-04 10:57

Does anyone know of a freely available java 1.5 package that provides a list of ISO 3166-1 country codes as a enum or EnumMap? Specifically I need the \"ISO 3166-1-alpha-2

11条回答
  •  清歌不尽
    2020-12-04 11:14

    If you are already going to rely on Java locale, then I suggest using a simple HashMap instead of creating new classes for countries etc.

    Here's how I would use it if I were to rely on the Java Localization only:

    private HashMap countries = new HashMap();
    String[] countryCodes = Locale.getISOCountries();
    
    for (String cc : countryCodes) {
        // country name , country code map
        countries.put(new Locale("", cc).getDisplayCountry(), cc.toUpperCase());
    }
    

    After you fill the map, you can get the ISO code from the country name whenever you need it. Or you can make it a ISO code to Country name map as well, just modify the 'put' method accordingly.

提交回复
热议问题