Languages
[Edit]
EN

Java - list of ISO 3166 country codes

6 points
Created by:
Kate_C
22810

In this article, we would like to present simple Java version of countries and associated codes to them described by ISO 3166.

The article contains two lists:

  1. one ossociated name per county codes ,
  2. many associated country names per country code - go to second example.

Note: the below list:

Country list as CountryCodeEnum.java file:

package example;

import java.util.HashMap;
import java.util.Map;

public enum CountryCodeEnum {

	AF("AF", "Afghanistan"),
	AX("AX", "Ă…land Islands"),
	AL("AL", "Albania"),
	DZ("DZ", "Algeria"),
	AS("AS", "American Samoa"),
	AD("AD", "Andorra"),
	AO("AO", "Angola"),
	AI("AI", "Anguilla"),
	AQ("AQ", "Antarctica"),
	AG("AG", "Antigua and Barbuda"),
	AR("AR", "Argentina"),
	AM("AM", "Armenia"),
	AW("AW", "Aruba"),
	AU("AU", "Australia"),
	AT("AT", "Austria"),
	AZ("AZ", "Azerbaijan"),
	BS("BS", "Bahamas"),
	BH("BH", "Bahrain"),
	BD("BD", "Bangladesh"),
	BB("BB", "Barbados"),
	BY("BY", "Belarus"),
	BE("BE", "Belgium"),
	BZ("BZ", "Belize"),
	BJ("BJ", "Benin"),
	BM("BM", "Bermuda"),
	BT("BT", "Bhutan"),
	BO("BO", "Bolivia (Plurinational State of)"),
	BQ("BQ", "Bonaire, Sint Eustatius and Saba"),
	BA("BA", "Bosnia and Herzegovina"),
	BW("BW", "Botswana"),
	BV("BV", "Bouvet Island"),
	BR("BR", "Brazil"),
	IO("IO", "British Indian Ocean Territory"),
	BN("BN", "Brunei Darussalam"),
	BG("BG", "Bulgaria"),
	BF("BF", "Burkina Faso"),
	BI("BI", "Burundi"),
	CV("CV", "Cabo Verde"),
	KH("KH", "Cambodia"),
	CM("CM", "Cameroon"),
	CA("CA", "Canada"),
	KY("KY", "Cayman Islands"),
	CF("CF", "Central African Republic"),
	TD("TD", "Chad"),
	CL("CL", "Chile"),
	CN("CN", "China"),
	CX("CX", "Christmas Island"),
	CC("CC", "Cocos (Keeling) Islands"),
	CO("CO", "Colombia"),
	KM("KM", "Comoros"),
	CG("CG", "Congo"),
	CD("CD", "Congo (Democratic Republic of the)"),
	CK("CK", "Cook Islands"),
	CR("CR", "Costa Rica"),
	CI("CI", "CĂ´te d'Ivoire"),
	HR("HR", "Croatia"),
	CU("CU", "Cuba"),
	CW("CW", "Curaçao"),
	CY("CY", "Cyprus"),
	CZ("CZ", "Czech Republic"),
	DK("DK", "Denmark"),
	DJ("DJ", "Djibouti"),
	DM("DM", "Dominica"),
	DO("DO", "Dominican Republic"),
	EC("EC", "Ecuador"),
	EG("EG", "Egypt"),
	SV("SV", "El Salvador"),
	GQ("GQ", "Equatorial Guinea"),
	ER("ER", "Eritrea"),
	EE("EE", "Estonia"),
	ET("ET", "Ethiopia"),
	FK("FK", "Falkland Islands (Malvinas)"),
	FO("FO", "Faroe Islands"),
	FJ("FJ", "Fiji (Republic of)"),
	FI("FI", "Finland"),
	FR("FR", "France"),
	GF("GF", "French Guiana"),
	PF("PF", "French Polynesia"),
	TF("TF", "French Southern Territories"),
	GA("GA", "Gabon"),
	GM("GM", "Gambia"),
	GE("GE", "Georgia"),
	DE("DE", "Germany"),
	GH("GH", "Ghana"),
	GI("GI", "Gibraltar"),
	GR("GR", "Greece"),
	GL("GL", "Greenland"),
	GD("GD", "Grenada"),
	GP("GP", "Guadeloupe"),
	GU("GU", "Guam"),
	GT("GT", "Guatemala"),
	GG("GG", "Guernsey"),
	GN("GN", "Guinea"),
	GW("GW", "Guinea-Bissau"),
	GY("GY", "Guyana"),
	HT("HT", "Haiti"),
	HM("HM", "Heard Island and McDonald Islands"),
	VA("VA", "Holy See"),
	HN("HN", "Honduras"),
	HK("HK", "Hong Kong"),
	HU("HU", "Hungary"),
	IS("IS", "Iceland"),
	IN("IN", "India"),
	ID("ID", "Indonesia"),
	IR("IR", "Iran (Islamic Republic of)"),
	IQ("IQ", "Iraq"),
	IE("IE", "Ireland"),
	IM("IM", "Isle of Man"),
	IL("IL", "Israel"),
	IT("IT", "Italy"),
	JM("JM", "Jamaica"),
	JP("JP", "Japan"),
	JE("JE", "Jersey"),
	JO("JO", "Jordan"),
	KZ("KZ", "Kazakhstan"),
	KE("KE", "Kenya"),
	KI("KI", "Kiribati"),
	KP("KP", "Korea (Democratic People's Republic of)"),
	KR("KR", "Korea (Republic of)"),
	KW("KW", "Kuwait"),
	KG("KG", "Kyrgyzstan"),
	LA("LA", "Lao People's Democratic Republic"),
	LV("LV", "Latvia"),
	LB("LB", "Lebanon"),
	LS("LS", "Lesotho"),
	LR("LR", "Liberia"),
	LY("LY", "Libya"),
	LI("LI", "Liechtenstein"),
	LT("LT", "Lithuania"),
	LU("LU", "Luxembourg"),
	MO("MO", "Macao"),
	MK("MK", "Macedonia (the former Yugoslav Republic of)"),
	MG("MG", "Madagascar"),
	MW("MW", "Malawi"),
	MY("MY", "Malaysia"),
	MV("MV", "Maldives"),
	ML("ML", "Mali"),
	MT("MT", "Malta"),
	MH("MH", "Marshall Islands"),
	MQ("MQ", "Martinique"),
	MR("MR", "Mauritania"),
	MU("MU", "Mauritius"),
	YT("YT", "Mayotte"),
	MX("MX", "Mexico"),
	FM("FM", "Micronesia (Federated States of)"),
	MD("MD", "Moldova (Republic of)"),
	MC("MC", "Monaco"),
	MN("MN", "Mongolia"),
	ME("ME", "Montenegro"),
	MS("MS", "Montserrat"),
	MA("MA", "Morocco"),
	MZ("MZ", "Mozambique"),
	MM("MM", "Myanmar"),
	NA("NA", "Namibia"),
	NR("NR", "Nauru"),
	NP("NP", "Nepal"),
	NL("NL", "Netherlands"),
	NC("NC", "New Caledonia"),
	NZ("NZ", "New Zealand"),
	NI("NI", "Nicaragua"),
	NE("NE", "Niger"),
	NG("NG", "Nigeria"),
	NU("NU", "Niue"),
	NF("NF", "Norfolk Island"),
	MP("MP", "Northern Mariana Islands"),
	NO("NO", "Norway"),
	OM("OM", "Oman"),
	PK("PK", "Pakistan"),
	PW("PW", "Palau"),
	PS("PS", "Palestine, State of"),
	PA("PA", "Panama"),
	PG("PG", "Papua New Guinea"),
	PY("PY", "Paraguay"),
	PE("PE", "Peru"),
	PH("PH", "Philippines"),
	PN("PN", "Pitcairn"),
	PL("PL", "Poland"),
	PT("PT", "Portugal"),
	PR("PR", "Puerto Rico"),
	QA("QA", "Qatar"),
	RE("RE", "RĂ©union"),
	RO("RO", "Romania"),
	RU("RU", "Russian Federation"),
	RW("RW", "Rwanda"),
	BL("BL", "Saint Barthélemy"),
	SH("SH", "Saint Helena, Ascension and Tristan da Cunha"),
	KN("KN", "Saint Kitts and Nevis"),
	LC("LC", "Saint Lucia"),
	MF("MF", "Saint Martin (French part)"),
	PM("PM", "Saint Pierre and Miquelon"),
	VC("VC", "Saint Vincent and the Grenadines"),
	WS("WS", "Samoa"),
	SM("SM", "San Marino"),
	ST("ST", "Sao Tome and Principe"),
	SA("SA", "Saudi Arabia"),
	SN("SN", "Senegal"),
	RS("RS", "Serbia"),
	SC("SC", "Seychelles"),
	SL("SL", "Sierra Leone"),
	SG("SG", "Singapore"),
	SX("SX", "Sint Maarten (Dutch part)"),
	SK("SK", "Slovakia"),
	SI("SI", "Slovenia"),
	SB("SB", "Solomon Islands"),
	SO("SO", "Somalia"),
	ZA("ZA", "South Africa"),
	GS("GS", "South Georgia and the South Sandwich Islands"),
	SS("SS", "South Sudan"),
	ES("ES", "Spain"),
	LK("LK", "Sri Lanka"),
	SD("SD", "Sudan"),
	SR("SR", "Suriname"),
	SJ("SJ", "Svalbard and Jan Mayen"),
	SZ("SZ", "Swaziland"),
	SE("SE", "Sweden"),
	CH("CH", "Switzerland"),
	SY("SY", "Syrian Arab Republic"),
	TW("TW", "Taiwan, Province of China"),
	TJ("TJ", "Tajikistan"),
	TZ("TZ", "Tanzania, United Republic of"),
	TH("TH", "Thailand"),
	TL("TL", "Timor-Leste"),
	TG("TG", "Togo"),
	TK("TK", "Tokelau"),
	TO("TO", "Tonga"),
	TT("TT", "Trinidad and Tobago"),
	TN("TN", "Tunisia"),
	TR("TR", "Turkey"),
	TM("TM", "Turkmenistan"),
	TC("TC", "Turks and Caicos Islands"),
	TV("TV", "Tuvalu"),
	UG("UG", "Uganda"),
	UA("UA", "Ukraine"),
	AE("AE", "United Arab Emirates"),
	GB("GB", "United Kingdom of Great Britain and Northern Ireland"),
	US("US", "United States of America"),
	UM("UM", "United States Minor Outlying Islands"),
	UY("UY", "Uruguay"),
	UZ("UZ", "Uzbekistan"),
	VU("VU", "Vanuatu"),
	VE("VE", "Venezuela (Bolivarian Republic of)"),
	VN("VN", "Vietnam"),
	VG("VG", "Virgin Islands (British)"),
	VI("VI", "Virgin Islands (U.S.)"),
	WF("WF", "Wallis and Futuna"),
	EH("EH", "Western Sahara"),
	YE("YE", "Yemen"),
	ZM("ZM", "Zambia"),
	ZW("ZW", "Zimbabwe");

    private static final Map<String, CountryCodeEnum> codeMap = new HashMap<>();
    private static final Map<String, CountryCodeEnum> nameMap = new HashMap<>();

    private String code;
    private String name;

    static {
        for (CountryCodeEnum el : values()) {
            codeMap.put(el.code, el);
            nameMap.put(el.name, el);
        }
    }

    CountryCodeEnum(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return this.code;
    }

    public String getName() {
        return this.name;
    }

    public static CountryCodeEnum findByCode(String code) {
        return codeMap.get(code);
    }

    public static CountryCodeEnum findByName(String name) {
        return nameMap.get(name);
    }
}

Usage example as Program.java file:

package example;

public class Program {

    public static void main(String[] args) {

        CountryEnum pl = CountryCodeEnum.findByCode("PL");
        CountryEnum gb = CountryCodeEnum.findByCode("GB");
        CountryEnum us = CountryCodeEnum.findByCode("US");
        CountryEnum jp = CountryCodeEnum.findByCode("JP");

        System.out.println(pl.getName()); // Poland
        System.out.println(gb.getName()); // United Kingdom of Great Britain and Northern Ireland
        System.out.println(us.getName()); // United States of America
        System.out.println(jp.getName()); // Japan
    }
}

Country codes with many alternative names

This section contains many alternative country names that are related with same country code.

The main idea of the country names is to organise them starting from simplest, across to some other alternatives, going to longest official names.

TODO: list requires to add more alternative names in future.

Country list as CountryCodeEnum.java file:

package examples;

import java.util.HashMap;
import java.util.Map;

public enum CountryCodeEnum {

    AF("AF", new String[] {"Afghanistan"}),
    AX("AX", new String[] {"Ă…land Islands"}),
    AL("AL", new String[] {"Albania"}),
    DZ("DZ", new String[] {"Algeria"}),
    AS("AS", new String[] {"American Samoa"}),
    AD("AD", new String[] {"Andorra"}),
    AO("AO", new String[] {"Angola"}),
    AI("AI", new String[] {"Anguilla"}),
    AQ("AQ", new String[] {"Antarctica"}),
    AG("AG", new String[] {"Antigua and Barbuda"}),
    AR("AR", new String[] {"Argentina"}),
    AM("AM", new String[] {"Armenia"}),
    AW("AW", new String[] {"Aruba"}),
    AU("AU", new String[] {"Australia"}),
    AT("AT", new String[] {"Austria"}),
    AZ("AZ", new String[] {"Azerbaijan"}),
    BS("BS", new String[] {"Bahamas"}),
    BH("BH", new String[] {"Bahrain"}),
    BD("BD", new String[] {"Bangladesh"}),
    BB("BB", new String[] {"Barbados"}),
    BY("BY", new String[] {"Belarus"}),
    BE("BE", new String[] {"Belgium"}),
    BZ("BZ", new String[] {"Belize"}),
    BJ("BJ", new String[] {"Benin"}),
    BM("BM", new String[] {"Bermuda"}),
    BT("BT", new String[] {"Bhutan"}),
    BO("BO", new String[] {"Bolivia", "Bolivia (Plurinational State of)", "Plurinational State of Bolivia"}),
    BQ("BQ", new String[] {"Bonaire, Sint Eustatius and Saba"}),
    BA("BA", new String[] {"Bosnia and Herzegovina"}),
    BW("BW", new String[] {"Botswana"}),
    BV("BV", new String[] {"Bouvet Island"}),
    BR("BR", new String[] {"Brazil"}),
    IO("IO", new String[] {"British Indian Ocean Territory"}),
    BN("BN", new String[] {"Brunei", "Nation of Brunei, the Abode of Peace", "Brunei Darussalam"}),
    BG("BG", new String[] {"Bulgaria"}),
    BF("BF", new String[] {"Burkina Faso"}),
    BI("BI", new String[] {"Burundi"}),
    CV("CV", new String[] {"Cabo Verde", "Cape Verde", "Republic of Cabo Verde"}),
    KH("KH", new String[] {"Cambodia"}),
    CM("CM", new String[] {"Cameroon"}),
    CA("CA", new String[] {"Canada"}),
    KY("KY", new String[] {"Cayman Islands"}),
    CF("CF", new String[] {"Central African Republic"}),
    TD("TD", new String[] {"Chad"}),
    CL("CL", new String[] {"Chile"}),
    CN("CN", new String[] {"China", "PRC", "People\'s Republic of China"}),
    CX("CX", new String[] {"Christmas Island"}),
    CC("CC", new String[] {"Cocos (Keeling) Islands"}),
    CO("CO", new String[] {"Colombia"}),
    KM("KM", new String[] {"Comoros"}),
    CG("CG", new String[] {"Congo", "Republic of the Congo"}),
    CD("CD", new String[] {"Congo (Democratic Republic of the)", "DRC", "Congo-Kinshasa", "DR Congo", "the DROC", "the Congo", "Democratic Republic of the Congo"}),
    CK("CK", new String[] {"Cook Islands"}),
    CR("CR", new String[] {"Costa Rica"}),
    CI("CI", new String[] {"CĂ´te d\'Ivoire", "Ivory Coast", "Republic of CĂ´te d\'Ivoire"}),
    HR("HR", new String[] {"Croatia"}),
    CU("CU", new String[] {"Cuba"}),
    CW("CW", new String[] {"Curaçao"}),
    CY("CY", new String[] {"Cyprus"}),
    CZ("CZ", new String[] {"Czech Republic"}),
    DK("DK", new String[] {"Denmark"}),
    DJ("DJ", new String[] {"Djibouti"}),
    DM("DM", new String[] {"Dominica"}),
    DO("DO", new String[] {"Dominican Republic"}),
    EC("EC", new String[] {"Ecuador"}),
    EG("EG", new String[] {"Egypt"}),
    SV("SV", new String[] {"El Salvador"}),
    GQ("GQ", new String[] {"Equatorial Guinea"}),
    ER("ER", new String[] {"Eritrea"}),
    EE("EE", new String[] {"Estonia"}),
    ET("ET", new String[] {"Ethiopia"}),
    FK("FK", new String[] {"Falkland Islands (Malvinas)"}),
    FO("FO", new String[] {"Faroe Islands"}),
    FJ("FJ", new String[] {"Fiji", "Fiji (Republic of)", "Republic of Fiji"}),
    FI("FI", new String[] {"Finland"}),
    FR("FR", new String[] {"France"}),
    GF("GF", new String[] {"French Guiana"}),
    PF("PF", new String[] {"French Polynesia"}),
    TF("TF", new String[] {"French Southern Territories"}),
    GA("GA", new String[] {"Gabon"}),
    GM("GM", new String[] {"Gambia"}),
    GE("GE", new String[] {"Georgia"}),
    DE("DE", new String[] {"Germany"}),
    GH("GH", new String[] {"Ghana"}),
    GI("GI", new String[] {"Gibraltar"}),
    GR("GR", new String[] {"Greece"}),
    GL("GL", new String[] {"Greenland"}),
    GD("GD", new String[] {"Grenada"}),
    GP("GP", new String[] {"Guadeloupe"}),
    GU("GU", new String[] {"Guam"}),
    GT("GT", new String[] {"Guatemala"}),
    GG("GG", new String[] {"Guernsey"}),
    GN("GN", new String[] {"Guinea"}),
    GW("GW", new String[] {"Guinea-Bissau"}),
    GY("GY", new String[] {"Guyana"}),
    HT("HT", new String[] {"Haiti"}),
    HM("HM", new String[] {"Heard Island and McDonald Islands"}),
    VA("VA", new String[] {"The Vatican", "Vatican City", "Vatican City State", "Holy See"}),
    HN("HN", new String[] {"Honduras"}),
    HK("HK", new String[] {"Hong Kong"}),
    HU("HU", new String[] {"Hungary"}),
    IS("IS", new String[] {"Iceland"}),
    IN("IN", new String[] {"India"}),
    ID("ID", new String[] {"Indonesia"}),
    IR("IR", new String[] {"Iran", "Iran (Islamic Republic of)"}),
    IQ("IQ", new String[] {"Iraq"}),
    IE("IE", new String[] {"Ireland"}),
    IM("IM", new String[] {"Isle of Man"}),
    IL("IL", new String[] {"Israel"}),
    IT("IT", new String[] {"Italy"}),
    JM("JM", new String[] {"Jamaica"}),
    JP("JP", new String[] {"Japan"}),
    JE("JE", new String[] {"Jersey"}),
    JO("JO", new String[] {"Jordan"}),
    KZ("KZ", new String[] {"Kazakhstan"}),
    KE("KE", new String[] {"Kenya"}),
    KI("KI", new String[] {"Kiribati"}),
    KP("KP", new String[] {"North Korea", "DPRK", "DPR Korea", "Korea (Democratic People\'s Republic of)", "Democratic People\'s Republic of Korea"}),
    KR("KR", new String[] {"South Korea", "ROK", "Korea (Republic of)", "Republic of Korea"}),
    KW("KW", new String[] {"Kuwait"}),
    KG("KG", new String[] {"Kyrgyzstan"}),
    LA("LA", new String[] {"Laos", "Lao People\'s Democratic Republic"}),
    LV("LV", new String[] {"Latvia"}),
    LB("LB", new String[] {"Lebanon"}),
    LS("LS", new String[] {"Lesotho"}),
    LR("LR", new String[] {"Liberia"}),
    LY("LY", new String[] {"Libya"}),
    LI("LI", new String[] {"Liechtenstein"}),
    LT("LT", new String[] {"Lithuania"}),
    LU("LU", new String[] {"Luxembourg"}),
    MO("MO", new String[] {"Macao"}),
    MK("MK", new String[] {"Macedonia", "Macedonia (the former Yugoslav Republic of)", "North Macedonia", "Republic of North Macedonia"}),
    MG("MG", new String[] {"Madagascar"}),
    MW("MW", new String[] {"Malawi"}),
    MY("MY", new String[] {"Malaysia"}),
    MV("MV", new String[] {"Maldives"}),
    ML("ML", new String[] {"Mali"}),
    MT("MT", new String[] {"Malta"}),
    MH("MH", new String[] {"Marshall Islands"}),
    MQ("MQ", new String[] {"Martinique"}),
    MR("MR", new String[] {"Mauritania"}),
    MU("MU", new String[] {"Mauritius"}),
    YT("YT", new String[] {"Mayotte"}),
    MX("MX", new String[] {"Mexico"}),
    FM("FM", new String[] {"Micronesia", "FSM", "Micronesia (Federated States of)", "Federated States of Micronesia"}),
    MD("MD", new String[] {"Moldova", "Moldova (Republic of)", "Republic of Moldova"}),
    MC("MC", new String[] {"Monaco"}),
    MN("MN", new String[] {"Mongolia"}),
    ME("ME", new String[] {"Montenegro"}),
    MS("MS", new String[] {"Montserrat"}),
    MA("MA", new String[] {"Morocco"}),
    MZ("MZ", new String[] {"Mozambique"}),
    MM("MM", new String[] {"Myanmar"}),
    NA("NA", new String[] {"Namibia"}),
    NR("NR", new String[] {"Nauru"}),
    NP("NP", new String[] {"Nepal"}),
    NL("NL", new String[] {"Netherlands"}),
    NC("NC", new String[] {"New Caledonia"}),
    NZ("NZ", new String[] {"New Zealand"}),
    NI("NI", new String[] {"Nicaragua"}),
    NE("NE", new String[] {"Niger"}),
    NG("NG", new String[] {"Nigeria"}),
    NU("NU", new String[] {"Niue"}),
    NF("NF", new String[] {"Norfolk Island"}),
    MP("MP", new String[] {"Northern Mariana Islands"}),
    NO("NO", new String[] {"Norway"}),
    OM("OM", new String[] {"Oman"}),
    PK("PK", new String[] {"Pakistan"}),
    PW("PW", new String[] {"Palau"}),
    PS("PS", new String[] {"Palestine", "Palestine, State of", "State of Palestine"}),
    PA("PA", new String[] {"Panama"}),
    PG("PG", new String[] {"Papua New Guinea"}),
    PY("PY", new String[] {"Paraguay"}),
    PE("PE", new String[] {"Peru"}),
    PH("PH", new String[] {"Philippines"}),
    PN("PN", new String[] {"Pitcairn"}),
    PL("PL", new String[] {"Poland"}),
    PT("PT", new String[] {"Portugal"}),
    PR("PR", new String[] {"Puerto Rico"}),
    QA("QA", new String[] {"Qatar"}),
    RE("RE", new String[] {"RĂ©union"}),
    RO("RO", new String[] {"Romania"}),
    RU("RU", new String[] {"Russia", "Russian Federation"}),
    RW("RW", new String[] {"Rwanda"}),
    BL("BL", new String[] {"Saint Barthélemy"}),
    SH("SH", new String[] {"Saint Helena, Ascension and Tristan da Cunha"}),
    KN("KN", new String[] {"Saint Kitts and Nevis"}),
    LC("LC", new String[] {"Saint Lucia"}),
    MF("MF", new String[] {"Saint Martin (French part)"}),
    PM("PM", new String[] {"Saint Pierre and Miquelon"}),
    VC("VC", new String[] {"Saint Vincent and the Grenadines"}),
    WS("WS", new String[] {"Samoa"}),
    SM("SM", new String[] {"San Marino"}),
    ST("ST", new String[] {"Sao Tome and Principe", "Saint Thomas and Prince", "São Tomé and Príncipe", "Democratic Republic of São Tomé and Príncipe"}),
    SA("SA", new String[] {"Saudi Arabia"}),
    SN("SN", new String[] {"Senegal"}),
    RS("RS", new String[] {"Serbia"}),
    SC("SC", new String[] {"Seychelles"}),
    SL("SL", new String[] {"Sierra Leone"}),
    SG("SG", new String[] {"Singapore"}),
    SX("SX", new String[] {"Sint Maarten (Dutch part)"}),
    SK("SK", new String[] {"Slovakia"}),
    SI("SI", new String[] {"Slovenia"}),
    SB("SB", new String[] {"Solomon Islands"}),
    SO("SO", new String[] {"Somalia"}),
    ZA("ZA", new String[] {"South Africa"}),
    GS("GS", new String[] {"South Georgia and the South Sandwich Islands"}),
    SS("SS", new String[] {"South Sudan"}),
    ES("ES", new String[] {"Spain"}),
    LK("LK", new String[] {"Sri Lanka"}),
    SD("SD", new String[] {"Sudan"}),
    SR("SR", new String[] {"Suriname"}),
    SJ("SJ", new String[] {"Svalbard and Jan Mayen"}),
    SZ("SZ", new String[] {"Swaziland"}),
    SE("SE", new String[] {"Sweden"}),
    CH("CH", new String[] {"Switzerland"}),
    SY("SY", new String[] {"Syria", "Syrian Arab Republic"}),
    TW("TW", new String[] {"Taiwan", "Taiwan, Province of China", "Republic of China (Taiwan)"}),
    TJ("TJ", new String[] {"Tajikistan"}),
    TZ("TZ", new String[] {"Tanzania", "Tanzania, United Republic of", "United Republic of Tanzania"}),
    TH("TH", new String[] {"Thailand"}),
    TL("TL", new String[] {"Timor-Leste"}),
    TG("TG", new String[] {"Togo"}),
    TK("TK", new String[] {"Tokelau"}),
    TO("TO", new String[] {"Tonga"}),
    TT("TT", new String[] {"Trinidad and Tobago"}),
    TN("TN", new String[] {"Tunisia"}),
    TR("TR", new String[] {"Turkey"}),
    TM("TM", new String[] {"Turkmenistan"}),
    TC("TC", new String[] {"Turks and Caicos Islands"}),
    TV("TV", new String[] {"Tuvalu"}),
    UG("UG", new String[] {"Uganda"}),
    UA("UA", new String[] {"Ukraine"}),
    AE("AE", new String[] {"United Arab Emirates"}),
    GB("GB", new String[] {"United Kingdom", "UK", "U.K.", "United Kingdom of Great Britain and Northern Ireland"}),
    US("US", new String[] {"United States", "US", "U.S.", "USA", "America", "United States of America"}),
    UM("UM", new String[] {"United States Minor Outlying Islands"}),
    UY("UY", new String[] {"Uruguay"}),
    UZ("UZ", new String[] {"Uzbekistan"}),
    VU("VU", new String[] {"Vanuatu"}),
    VE("VE", new String[] {"Venezuela", "Venezuela (Bolivarian Republic of)"}),
    VN("VN", new String[] {"Vietnam"}),
    VG("VG", new String[] {"Virgin Islands (British)"}),
    VI("VI", new String[] {"Virgin Islands (U.S.)"}),
    WF("WF", new String[] {"Wallis and Futuna"}),
    EH("EH", new String[] {"Western Sahara"}),
    YE("YE", new String[] {"Yemen"}),
    ZM("ZM", new String[] {"Zambia"}),
    ZW("ZW", new String[] {"Zimbabwe"});

    private static final Map<String, CountryCodeEnum> codeMap = new HashMap<>();
    private static final Map<String, CountryCodeEnum> nameMap = new HashMap<>();

    private String code;
    private String[] names;

    static {
        for (CountryCodeEnum country : values()) {
            String code = country.getCode();
            String[] names = country.getNames();
            if (names.length == 0) {
                throw new RuntimeException("Required at least one country name (" + code + ").");
            }
            codeMap.put(code, country);
            for (String name : names) {
                String key = name.toLowerCase();
                CountryCodeEnum value = nameMap.put(key, country);
                if (value != null) {
                    throw new RuntimeException("Country name is duplicated (" code + ": " + name + ").");
                }
            }
        }
    }

    CountryCodeEnum(String code, String[] names) {
        this.code = code;
        this.names = names;
    }

    public String getCode() {
        return this.code;
    }

    public String getName() {
        return this.names[0];
    }

    public String[] getNames() {
        return this.names;
    }

    public static CountryCodeEnum findByName(String name) {
        return nameMap.get(name.toLowerCase());
    }

    public static CountryCodeEnum findByCode(String code) {
        return codeMap.get(code);
    }
}

 Usage example as Program.java file:

package example;

public class Program {

    public static void main(String[] args) {

        CountryEnum pl = CountryCodeEnum.findByCode("PL");
        CountryEnum gb = CountryCodeEnum.findByCode("GB");
        CountryEnum us = CountryCodeEnum.findByName("United States of America");
        CountryEnum jp = CountryCodeEnum.findByName("Japan");

        System.out.println(pl.getName()); // Poland
        System.out.println(gb.getName()); // United Kingdom of Great Britain and Northern Ireland
        System.out.println(us.getName()); // United States of America
        System.out.println(jp.getName()); // Japan
    }
}
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join