How do I get a list of country names?

Using the code below you can get a list of country names and its ISO code by using the locale class implementation in Java.

package org.kodejava.example.util;

import java.text.Collator;
import java.util.*;

public class CountryList {
    public static void main(String[] args) {
        // A collection to store our country object
        List<Country> countries = new ArrayList<Country>();

        // Get ISO countries, create Country object and
        // store in the collection.
        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();

            if (!"".equals(iso) && !"".equals(code)
                    && !"".equals(name)) {
                countries.add(new Country(iso, code, name));
            }
        }

        // Sort the country by their name and then display the content
        // of countries collection object.
        Collections.sort(countries, new CountryComparator());

        for (Country country : countries) {
            System.out.println(country);
        }
    }

    /**
     * Country pojo class.
     */
    static class Country {
        private String iso;
        private String code;
        private String name;

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

        public String toString() {
            return iso + " - " + code + " - " + name.toUpperCase();
        }
    }

    /**
     * CountryComparator class.
     */
    private static class CountryComparator implements Comparator<Country> {
        private Comparator<Object> comparator;

        CountryComparator() {
            comparator = Collator.getInstance();
        }

        public int compare(Country c1, Country c2) {
            return comparator.compare(c1.name, c2.name);
        }
    }
}

Here are the list of countries we got:

ISO CODE NAME
AFG AF Afghanistan
ALA AX Åland Islands
ALB AL Albania
DZA DZ Algeria
ASM AS American Samoa
AND AD Andorra
AGO AO Angola
AIA AI Anguilla
ATA AQ Antarctica
ATG AG Antigua and Barbuda
ARG AR Argentina
ARM AM Armenia
ABW AW Aruba
AUS AU Australia
AUT AT Austria
AZE AZ Azerbaijan
BHS BS Bahamas
BHR BH Bahrain
BGD BD Bangladesh
BRB BB Barbados
BLR BY Belarus
BEL BE Belgium
BLZ BZ Belize
BEN BJ Benin
BMU BM Bermuda
BTN BT Bhutan
BOL BO Bolivia
BES BQ Bonaire, Sint Eustatius and Saba
BIH BA Bosnia and Herzegovina
BWA BW Botswana
BVT BV Bouvet Island
BRA BR Brazil
IOT IO British Indian Ocean Territory
VGB VG British Virgin Islands
BRN BN Brunei
BGR BG Bulgaria
BFA BF Burkina Faso
BDI BI Burundi
KHM KH Cambodia
CMR CM Cameroon
CAN CA Canada
CPV CV Cape Verde
CYM KY Cayman Islands
CAF CF Central African Republic
TCD TD Chad
CHL CL Chile
CHN CN China
CXR CX Christmas Island
CCK CC Cocos Islands
COL CO Colombia
COM KM Comoros
COG CG Congo
COK CK Cook Islands
CRI CR Costa Rica
CIV CI Côte d’Ivoire
HRV HR Croatia
CUB CU Cuba
CUW CW Curaçao
CYP CY Cyprus
CZE CZ Czech Republic
DNK DK Denmark
DJI DJ Djibouti
DMA DM Dominica
DOM DO Dominican Republic
ECU EC Ecuador
EGY EG Egypt
SLV SV El Salvador
GNQ GQ Equatorial Guinea
ERI ER Eritrea
EST EE Estonia
ETH ET Ethiopia
FLK FK Falkland Islands
FRO FO Faroe Islands
FJI FJ Fiji
FIN FI Finland
FRA FR France
GUF GF French Guiana
PYF PF French Polynesia
ATF TF French Southern Territories
GAB GA Gabon
GMB GM Gambia
GEO GE Georgia
DEU DE Germany
GHA GH Ghana
GIB GI Gibraltar
GRC GR Greece
GRL GL Greenland
GRD GD Grenada
GLP GP Guadeloupe
GUM GU Guam
GTM GT Guatemala
GGY GG Guernsey
GIN GN Guinea
GNB GW Guinea-Bissau
GUY GY Guyana
HTI HT Haiti
HMD HM Heard Island And McDonald Islands
HND HN Honduras
HKG HK Hong Kong
HUN HU Hungary
ISL IS Iceland
IND IN India
IDN ID Indonesia
IRN IR Iran
IRQ IQ Iraq
IRL IE Ireland
IMN IM Isle Of Man
ISR IL Israel
ITA IT Italy
JAM JM Jamaica
JPN JP Japan
JEY JE Jersey
JOR JO Jordan
KAZ KZ Kazakhstan
KEN KE Kenya
KIR KI Kiribati
KWT KW Kuwait
KGZ KG Kyrgyzstan
LAO LA Laos
LVA LV Latvia
LBN LB Lebanon
LSO LS Lesotho
LBR LR Liberia
LBY LY Libya
LIE LI Liechtenstein
LTU LT Lithuania
LUX LU Luxembourg
MAC MO Macao
MKD MK Macedonia
MDG MG Madagascar
MWI MW Malawi
MYS MY Malaysia
MDV MV Maldives
MLI ML Mali
MLT MT Malta
MHL MH Marshall Islands
MTQ MQ Martinique
MRT MR Mauritania
MUS MU Mauritius
MYT YT Mayotte
MEX MX Mexico
FSM FM Micronesia
MDA MD Moldova
MCO MC Monaco
MNG MN Mongolia
MNE ME Montenegro
MSR MS Montserrat
MAR MA Morocco
MOZ MZ Mozambique
MMR MM Myanmar
NAM NA Namibia
NRU NR Nauru
NPL NP Nepal
NLD NL Netherlands
ANT AN Netherlands Antilles
NCL NC New Caledonia
NZL NZ New Zealand
NIC NI Nicaragua
NER NE Niger
NGA NG Nigeria
NIU NU Niue
NFK NF Norfolk Island
MNP MP Northern Mariana Islands
PRK KP North Korea
NOR NO Norway
OMN OM Oman
PAK PK Pakistan
PLW PW Palau
PSE PS Palestine
PAN PA Panama
PNG PG Papua New Guinea
PRY PY Paraguay
PER PE Peru
PHL PH Philippines
PCN PN Pitcairn
POL PL Poland
PRT PT Portugal
PRI PR Puerto Rico
QAT QA Qatar
REU RE Reunion
ROU RO Romania
RUS RU Russia
RWA RW Rwanda
BLM BL Saint Barthélemy
SHN SH Saint Helena
KNA KN Saint Kitts And Nevis
LCA LC Saint Lucia
MAF MF Saint Martin
SPM PM Saint Pierre And Miquelon
VCT VC Saint Vincent And The Grenadines
WSM WS Samoa
SMR SM San Marino
STP ST Sao Tome And Principe
SAU SA Saudi Arabia
SEN SN Senegal
SRB RS Serbia
SYC SC Seychelles
SLE SL Sierra Leone
SGP SG Singapore
SXM SX Sint Maarten (Dutch part)
SVK SK Slovakia
SVN SI Slovenia
SLB SB Solomon Islands
SOM SO Somalia
ZAF ZA South Africa
SGS GS South Georgia And The South Sandwich Islands
KOR KR South Korea
SSD SS South Sudan
ESP ES Spain
LKA LK Sri Lanka
SDN SD Sudan
SUR SR Suriname
SJM SJ Svalbard And Jan Mayen
SWZ SZ Swaziland
SWE SE Sweden
CHE CH Switzerland
SYR SY Syria
TWN TW Taiwan
TJK TJ Tajikistan
TZA TZ Tanzania
THA TH Thailand
COD CD The Democratic Republic Of Congo
TLS TL Timor-Leste
TGO TG Togo
TKL TK Tokelau
TON TO Tonga
TTO TT Trinidad and Tobago
TUN TN Tunisia
TUR TR Turkey
TKM TM Turkmenistan
TCA TC Turks And Caicos Islands
TUV TV Tuvalu
VIR VI U.S. Virgin Islands
UGA UG Uganda
UKR UA Ukraine
ARE AE United Arab Emirates
GBR GB United Kingdom
USA US United States
UMI UM United States Minor Outlying Islands
URY UY Uruguay
UZB UZ Uzbekistan
VUT VU Vanuatu
VAT VA Vatican
VEN VE Venezuela
VNM VN Vietnam
WLF WF Wallis And Futuna
ESH EH Western Sahara
YEM YE Yemen
ZMB ZM Zambia
ZWE ZW Zimbabwe