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.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<>();

        // 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.
        countries.sort(new CountryComparator());

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

    /**
     * Country pojo class.
     */
    static class Country {
        private final String iso;
        private final String code;
        private final 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 final 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