-->

ISO2 country code from country name

2019-01-23 01:03发布

问题:

Is there an easy way in Java to get the ISO2 code from a given country name, for example "CH" when given "Switzerland"?

The only solution I can think of at the moment is to save all the country codes and names in an array and iterate over it. Any other (easier) solutions?

回答1:

You could use the built-in Locale class:

Locale l = new Locale("", "CH");
System.out.println(l.getDisplayCountry());

prints "Switzerland" for example. Note that I have not provided a language.

So what you can do for the reverse lookup is build a map from the available countries:

public static void main(String[] args) throws InterruptedException {
    Map<String, String> countries = new HashMap<>();
    for (String iso : Locale.getISOCountries()) {
        Locale l = new Locale("", iso);
        countries.put(l.getDisplayCountry(), iso);
    }

    System.out.println(countries.get("Switzerland"));
    System.out.println(countries.get("Andorra"));
    System.out.println(countries.get("Japan"));
}


回答2:

As others have mentioned Map is the easiest way, here's what I wrote/used where I had the same problem.

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


 public class CountryCodes {
     final Map<String, String> map = new TreeMap<String, String>   (String.CASE_INSENSITIVE_ORDER);
    public CountryCodes() {

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

    }

     public String getCode(String country){
     String countryFound = map.get(country);
     if(countryFound==null){
             countryFound="UK";
     }
     return countryFound;
     }
}


回答3:

CountryCode enum contained in com.neovisionaries:nv-i18n:1.11 (or later) has findByName methods to list up country codes whose names match a given pattern.

List<CountryCode> findByName(String regex);
List<CountryCode> findByName(Pattern pattern);

To get "CH" from "Switzerland":

String code =  CountryCode.findByName("Switzerland").get(0).name();


GitHub

https://github.com/TakahikoKawasaki/nv-i18n

JavaDoc

http://takahikokawasaki.github.io/nv-i18n/

Maven

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.11</version>
</dependency>


回答4:

Use a Map, not an array, then you don't have to iterate over it, you just look it up.

O(1) vs O(n)

//Do this once
Map<String,Locale> map = new HashMap<String,Locale>();
for (Locale locale : Locale.getAvailableLocales()) {
  map.put(locale.getDisplayCountry(), locale);
}


回答5:

Nope, you've got the easy solution already: The array (or DB table, or a keyed structure like a hash map) solution is pretty much the standard way of doing this. I guess there may also be some web services available that can provide the list, but I think that would be overkill (its not worth relying on a third party system to be working for something this simple). Similar arrays would be used for lists of counties/provinces/states within a country if you needed that level of detail.

The only thing to beware of with that is if you're accepting the country name as user input, you'll need to watch for spelling errors, incorrect capitalisation, and using alternative names for countries (eg "Switzerland" could legitimately be referred to as "Schweiz" or "Suisse" or "The Swiss Confederation", or a number of other variants.

More commonly, user input is restricted to known country codes by providing a drop-list so that the user picks the country he wants and the program knows immediately that it is "CH". This also requires the same array to be in place, but is referenced in the other direction, and is more reliable (since there is only one possible code for each country).

Also note that although the list of countries in the world doesn't change much, it does change. You should make sure you keep it up-to-date.



回答6:

This is definitely the answer you are looking for, it is late but hopefully it will hepl someone

String[] isoCountries = Locale.getISOCountries();
    for (String country : isoCountries) {
        Locale locale = new Locale("en", country);
        String iso = locale.getISO3Country();
        String code = locale.getCountry();
        String name = locale.getDisplayCountry();
        System.out.println(iso + " " + code + " " + name);

    }

I have added the print statement so that you can get a good look at the result. You can substitute "CH" for that country variable in the third line and it will give you "Afghanistan".



回答7:

Use a java.util.Map instead of an array.



回答8:

concat flag to url(https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/0.8.2/flags/1x1/)

INSERT INTO `countries` (`name`, `code`, `c_code`, `flag`) VALUES

('Afghanistan', '+93', 'AF', 'af.svg'),
('Albania', '+355', 'AL', 'al.svg'),
('Algeria', '+213', 'DZ', 'dz.svg'),
('American Samoa', '+1 684', 'AS', 'as.svg'),
('Andorra', '+376', 'AD', 'ad.svg'),
('Angola', '+244', 'AO', 'ao.svg'),
('Anguilla', '+1 264', 'AI', 'ai.svg'),
('Antigua and Barbuda', '+1 268', 'AG', 'ag.svg'),
('Argentina', '+54', 'AR', 'ar.svg'),
('Armenia', '+374', 'AM', 'am.svg'),
('Aruba', '+297', 'AW', 'aw.svg'),
('Australia', '+61', 'AU', 'au.svg'),
('Austria', '+43', 'AT', 'at.svg'),
('Azerbaijan', '+994', 'AZ', 'az.svg'),
('Bahamas', '+1 242', 'BS', 'bs.svg'),
('Bahrain', '+973', 'BH', 'bh.svg'),
('Bangladesh', '+880', 'BD', 'bd.svg'),
('Barbados', '+1 246', 'BB', 'bb.svg'),
('Belarus', '+375', 'BY', 'by.svg'),
('Belgium', '+32', 'BE', 'be.svg'),
('Belize', '+501', 'BZ', 'bz.svg'),
('Benin', '+229', 'BJ', 'bj.svg'),
('Bermuda', '+1 441', 'BM', 'bm.svg'),
('Bhutan', '+975', 'BT', 'bt.svg'),
('Bolivia', '+591', 'BO', 'bo.svg'),
('Bosnia and Herzegovina', '+387', 'BA', 'ba.svg'),
('Botswana', '+267', 'BW', 'bw.svg'),
('Brazil', '+55', 'BR', 'br.svg'),
('British Indian Ocean Territory', '+246', 'IO', 'io.svg'),
('British Virgin Islands', '+1 284', 'VG', 'vg.svg'),
('Brunei', '+673', 'BN', 'bn.svg'),
('Bulgaria', '+359', 'BG', 'bg.svg'),
('Burkina Faso', '+226', 'BF', 'bf.svg'),
('Burundi', '+257', 'BI', 'bi.svg'),
('Cambodia', '+855', 'KH', 'kh.svg'),
('Cameroon', '+237', 'CM', 'cm.svg'),
('Canada', '+1', 'CA', 'ca.svg'),
('Cape Verde', '+238', 'CV', 'cv.svg'),
('Cayman Islands', '+ 345', 'KY', 'ky.svg'),
('Central African Republic', '+236', 'CF', 'cf.svg'),
('Chad', '+235', 'TD', 'td.svg'),
('Chile', '+56', 'CL', 'cl.svg'),
('China', '+86', 'CN', 'cn.svg'),
('Christmas Island', '+61', 'CX', 'cx.svg'),
('Colombia', '+57', 'CO', 'co.svg'),
('Comoros', '+269', 'KM', 'km.svg'),
('Congo', '+242', 'CG', 'cg.svg'),
('Congo, Dem. Rep. of (Zaire)', '+243', 'CD', 'cd.svg'),
('Cook Islands', '+682', 'CK', 'ck.svg'),
('Costa Rica', '+506', 'CR', 'cr.svg'),
('Croatia', '+385', 'HR', 'hr.svg'),
('Cuba', '+53', 'CU', 'cu.svg'),
('Curacao', '+599', 'CW', 'cw.svg'),
('Cyprus', '+537', 'CY', 'cy.svg'),
('Czech Republic', '+420', 'CZ', 'cz.svg'),
('Denmark', '+45', 'DK', 'dk.svg'),
('Djibouti', '+253', 'DJ', 'dj.svg'),
('Dominica', '+1 767', 'DM', 'dm.svg'),
('Dominican Republic', '+1 809', 'DO', 'do.svg'),
('East Timor', '+670', 'TR', 'tr.svg'),
('Ecuador', '+593', 'EC', 'ec.svg'),
('Egypt', '+20', 'EG', 'eg.svg'),
('El Salvador', '+503', 'SV', 'sv.svg'),
('Equatorial Guinea', '+240', 'GQ', 'gq.svg'),
('Eritrea', '+291', 'ER', 'er.svg'),
('Estonia', '+372', 'EE', 'ee.svg'),
('Ethiopia', '+251', 'ET', 'et.svg'),
('Falkland Islands', '+500', 'FK', 'fk.svg'),
('Faroe Islands', '+298', 'FO', 'fo.svg'),
('Fiji', '+679', 'FJ', 'fj.svg'),
('Finland', '+358', 'FI', 'fi.svg'),
('France', '+33', 'FR', 'fr.svg'),
('French Guiana', '+594', 'GF', 'gf.svg'),
('French Polynesia', '+689', 'PF', 'pf.svg'),
('Gabon', '+241', 'GA', 'ga.svg'),
('Gambia', '+220', 'GM', 'gm.svg'),
('Georgia', '+995', 'GE', 'ge.svg'),
('Germany', '+49', 'DE', 'de.svg'),
('Ghana', '+233', 'GH', 'gh.svg'),
('Gibraltar', '+350', 'GI', 'gi.svg'),
('Greece', '+30', 'GR', 'gr.svg'),
('Greenland', '+299', 'GL', 'gl.svg'),
('Grenada', '+1 473', 'GD', 'gd.svg'),
('Guadeloupe', '+590', 'GP', 'gp.svg'),
('Guam', '+1 671', 'GU', 'gu.svg'),
('Guatemala', '+502', 'GT', 'gt.svg'),
('Guinea', '+224', 'GN', 'gn.svg'),
('Guinea-Bissau', '+245', 'GW', 'gw.svg'),
('Guyana', '+595', 'GY', 'gy.svg'),
('Haiti', '+509', 'HT', 'ht.svg'),
('Honduras', '+504', 'HN', 'hn.svg'),
('Hong Kong', '+852', 'HK', 'hk.svg'),
('Hungary', '+36', 'HU', 'hu.svg'),
('Iceland', '+354', 'IS', 'is.svg'),
('India', '+91', 'IN', 'in.svg'),
('Indonesia', '+62', 'ID', 'id.svg'),
('Iran', '+98', 'IR', 'ir.svg'),
('Iraq', '+964', 'IQ', 'iq.svg'),
('Ireland', '+353', 'IE', 'ie.svg'),
('Israel', '+972', 'IL', 'il.svg'),
('Italy', '+39', 'IT', 'it.svg'),
('Ivory Coast', '+225', 'CI', 'ci.svg'),
('Jamaica', '+1 876', 'JM', 'jm.svg'),
('Japan', '+81', 'JP', 'jp.svg'),
('Jordan', '+962', 'JO', 'jo.svg'),
('Kazakhstan', '+7 7', 'KZ', 'kz.svg'),
('Kenya', '+254', 'KE', 'ke.svg'),
('Kiribati', '+686', 'KI', 'ki.svg'),
('Kuwait', '+965', 'KW', 'kw.svg'),
('Kyrgyzstan', '+996', 'KG', 'kg.svg'),
('Laos', '+856', 'LA', 'la.svg'),
('Latvia', '+371', 'LV', 'lv.svg'),
('Lebanon', '+961', 'LB', 'lb.svg'),
('Lesotho', '+266', 'LS', 'ls.svg'),
('Liberia', '+231', 'LR', 'lr.svg'),
('Libya', '+218', 'LY', 'ly.svg'),
('Liechtenstein', '+423', 'LI', 'li.svg'),
('Lithuania', '+370', 'LT', 'lt.svg'),
('Luxembourg', '+352', 'LU', 'lu.svg'),
('Macau', '+853', 'MO', 'mo.svg'),
('Macedonia', '+389', 'MK', 'mk.svg'),
('Madagascar', '+261', 'MG', 'mg.svg'),
('Malawi', '+265', 'MW', 'mw.svg'),
('Malaysia', '+60', 'MY', 'my.svg'),
('Maldives', '+960', 'MV', 'mv.svg'),
('Mali', '+223', 'ML', 'ml.svg'),
('Malta', '+356', 'MT', 'mt.svg'),
('Marshall Islands', '+692', 'MH', 'mh.svg'),
('Martinique', '+596', 'MQ', 'mq.svg'),
('Mauritania', '+222', 'MR', 'mr.svg'),
('Mauritius', '+230', 'MU', 'mu.svg'),
('Mayotte', '+262', 'YT', 'yt.svg'),
('Mexico', '+52', 'MX', 'mx.svg'),
('Micronesia', '+691', 'FM', 'fm.svg'),
('Moldova', '+373', 'MD', 'md.svg'),
('Monaco', '+377', 'MC', 'mc.svg'),
('Mongolia', '+976', 'MN', 'mn.svg'),
('Montenegro', '+382', 'ME', 'me.svg'),
('Montserrat', '+1664', 'MS', 'ms.svg'),
('Morocco', '+212', 'MA', 'ma.svg'),
('Myanmar', '+95', 'MM', 'mm.svg'),
('Namibia', '+264', 'NA', 'na.svg'),
('Nauru', '+674', 'NR', 'nr.svg'),
('Nepal', '+977', 'NP', 'np.svg'),
('Netherlands', '+31', 'NL', 'nl.svg'),
('Saint Kitts and Nevis', '+1', 'KN', 'kn.svg'),
('New Caledonia', '+687', 'NC', 'nc.svg'),
('New Zealand', '+64', 'NZ', 'nz.svg'),
('Nicaragua', '+505', 'NI', 'ni.svg'),
('Niger', '+227', 'NE', 'ne.svg'),
('Nigeria', '+234', 'NG', 'ng.svg'),
('Niue', '+683', 'NU', 'nu.svg'),
('Norfolk Island', '+672', 'NF', 'nf.svg'),
('North Korea', '+850', 'KP', 'kp.svg'),
('Northern Mariana Islands', '+1 670', 'MP', 'mp.svg'),
('Norway', '+47', 'NO', 'no.svg'),
('Oman', '+968', 'OM', 'om.svg'),
('Pakistan', '+92', 'PK', 'pk.svg'),
('Palau', '+680', 'PW', 'pw.svg'),
('Panama', '+507', 'PA', 'pa.svg'),
('Papua New Guinea', '+675', 'PG', 'pg.svg'),
('Paraguay', '+595', 'PY', 'py.svg'),
('Peru', '+51', 'PE', 'pe.svg'),
('Philippines', '+63', 'PH', 'ph.svg'),
('Poland', '+48', 'PL', 'pl.svg'),
('Portugal', '+351', 'PT', 'pt.svg'),
('Puerto Rico', '+1 787', 'PR', 'pr.svg'),
('Qatar', '+974', 'QA', 'qa.svg'),
('Reunion', '+262', 'RE', 're.svg'),
('Romania', '+40', 'RO', 'ro.svg'),
('Russia', '+7', 'RU', 'ru.svg'),
('Rwanda', '+250', 'RW', 'rw.svg'),
('Samoa', '+685', 'WS', 'ws.svg'),
('San Marino', '+378', 'SM', 'sm.svg'),
('Saudi Arabia', '+966', 'SA', 'sa.svg'),
('Senegal', '+221', 'SN', 'sn.svg'),
('Serbia', '+381', 'RS', 'rs.svg'),
('Seychelles', '+248', 'SC', 'sc.svg'),
('Sierra Leone', '+232', 'SL', 'sl.svg'),
('Singapore', '+65', 'SG', 'sg.svg'),
('Slovakia', '+421', 'SK', 'sk.svg'),
('Slovenia', '+386', 'SI', 'si.svg'),
('Solomon Islands', '+677', 'SB', 'sb.svg'),
('South Africa', '+27', 'ZA', 'za.svg'),
('South Georgia and the South Sandwich Islands', '+500', 'GS', 'gs.svg'),
('South Korea', '+82', 'KR', 'kr.svg'),
('Spain', '+34', 'ES', 'es.svg'),
('Sri Lanka', '+94', 'LK', 'lk.svg'),
('Sudan', '+249', 'SD', 'sd.svg'),
('Suriname', '+597', 'SR', 'sr.svg'),
('Swaziland', '+268', 'SZ', 'sz.svg'),
('Sweden', '+46', 'SE', 'se.svg'),
('Switzerland', '+41', 'CH', 'ch.svg'),
('Syria', '+963', 'SY', 'sy.svg'),
('Taiwan', '+886', 'TW', 'tw.svg'),
('Tajikistan', '+992', 'TJ', 'tj.svg'),
('Tanzania', '+255', 'TZ', 'tz.svg'),
('Thailand', '+66', 'TH', 'th.svg'),
('Timor Leste', '+670', 'TL', 'tl.svg'),
('Togo', '+228', 'TG', 'tg.svg'),
('Tokelau', '+690', 'TK', 'tk.svg'),
('Tonga', '+676', 'TO', 'to.svg'),
('Trinidad and Tobago', '+1 868', 'TT', 'tt.svg'),
('Tunisia', '+216', 'TN', 'tn.svg'),
('Turkey', '+90', 'TR', 'tr.svg'),
('Turkmenistan', '+993', 'TM', 'tm.svg'),
('Turks and Caicos Islands', '+1 649', 'TC', 'tc.svg'),
('Tuvalu', '+688', 'TV', 'tv.svg'),
('Uganda', '+256', 'UG', 'ug.svg'),
('Ukraine', '+380', 'UA', 'ua.svg'),
('United Arab Emirates', '+971', 'AE', 'ae.svg'),
('United Kingdom', '+44', 'GB', 'gb.svg'),
('United States', '+1', 'US', 'us.svg'),
('Uruguay', '+598', 'UY', 'uy.svg'),
('Uzbekistan', '+998', 'UZ', 'uz.svg'),
('Vanuatu', '+678', 'VU', 'vu.svg'),
('Venezuela', '+58', 'VE', 've.svg'),
('Vietnam', '+84', 'VN', 'vn.svg'),
('Wallis and Futuna', '+681', 'WF', 'wf.svg'),
('Yemen', '+967', 'YE', 'ye.svg'),
('Zambia', '+260', 'ZM', 'zm.svg'),
('Zimbabwe', '+263', 'ZW', 'zw.svg')


回答9:

public static void main(String[] args) throws Exception {
    System.out.println(getCountryCodeFromName("Switzerland"));
}

public static String getCountryCodeFromName(String country) {
    return Arrays.asList(Locale.getISOCountries())
            .stream()
            .map((s) -> new Locale("", s))
            .filter((l) -> l.getDisplayCountry().toLowerCase().equals(country.toLowerCase()))
            .findFirst()
            .map((l) -> l.getCountry().toLowerCase())
            .orElse("UNKNOWN");
}

note that you will walk the stream every time you call the method, it is more efficient to use a map or save the result in a lookup table