Load ISO 3166-1 List of Countries in Java

4 September 2012 By Praveen Macherla 1,546 views No Comment
8 Flares Twitter 1 Facebook 0 Google+ 7 8 Flares ×

List of countries as per the ISO 3166-1. Get the countries.csv file from here.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ISOCountry {
	public static void main(String[] args) {
		List<Country> countries = new ArrayList<Country>();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("countries.csv"));
			String line = "";
			while((line = br.readLine()) != null) {
				String[] tokens = line.split(",");
				if(tokens.length == 4) {
					countries.add(
					new Country(tokens[0], 
					tokens[1], 
					tokens[2], 
					Integer.valueOf(tokens[3])));
				}
			}
			Collections.sort(countries);
			for(Country c : countries) {
				System.out.print(String.format("%03d", c.getNumericCode()));
				System.out.println(" - " + c);
			}
				
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

class Country implements Comparable<Country> {
	public Country(String name, String alpha2,
			String alpha3, Integer numericCode) {
		this.name = name;
		this.alpha2Code = alpha2;
		this.alpha3Code = alpha3;
		this.numericCode = numericCode;
	}
	private int numericCode;
	private String alpha2Code;
	private String alpha3Code;
	private String name;
	
	public int getNumericCode() {
		return numericCode;
	}

	public void setNumericCode(int numericCode) {
		this.numericCode = numericCode;
	}

	public String getAlpha2Code() {
		return alpha2Code;
	}

	public void setAlpha2Code(String alpha2Code) {
		this.alpha2Code = alpha2Code;
	}

	public String getAlpha3Code() {
		return alpha3Code;
	}

	public void setAlpha3Code(String alpha3Code) {
		this.alpha3Code = alpha3Code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return alpha2Code + ", " + alpha3Code + ", " + name;
	}

	@Override
	public int compareTo(Country o) {
		if(this.numericCode < o.numericCode)
			return -1;
		if(this.numericCode > o.numericCode)
			return 1;
		return 0;
	}
}

Output:
004 – AF, AFG, Afghanistan
008 – AL, ALB, Albania
010 – AQ, ATA, Antarctica
012 – DZ, DZA, Algeria
016 – AS, ASM, American Samoa
020 – AD, AND, Andorra
024 – AO, AGO, Angola
028 – AG, ATG, Antigua and Barbuda
031 – AZ, AZE, Azerbaijan
032 – AR, ARG, Argentina
036 – AU, AUS, Australia
040 – AT, AUT, Austria
044 – BS, BHS, Bahamas
048 – BH, BHR, Bahrain
050 – BD, BGD, Bangladesh
051 – AM, ARM, Armenia
052 – BB, BRB, Barbados
056 – BE, BEL, Belgium
060 – BM, BMU, Bermuda
064 – BT, BTN, Bhutan
068 – BO, BOL, Bolivia
070 – BA, BIH, Bosnia and Herzegovina
072 – BW, BWA, Botswana
074 – BV, BVT, Bouvet Island
076 – BR, BRA, Brazil
084 – BZ, BLZ, Belize
086 – IO, IOT, British Indian Ocean Territory
090 – SB, SLB, Solomon Islands
092 – VG, VGB, Virgin Islands British
096 – BN, BRN, Brunei Darussalam
100 – BG, BGR, Bulgaria
104 – MM, MMR, Myanmar
108 – BI, BDI, Burundi
112 – BY, BLR, Belarus
116 – KH, KHM, Cambodia
120 – CM, CMR, Cameroon
124 – CA, CAN, Canada
132 – CV, CPV, Cape Verde
136 – KY, CYM, Cayman Islands
140 – CF, CAF, Central African Republic
144 – LK, LKA, Sri Lanka
148 – TD, TCD, Chad
152 – CL, CHL, Chile
156 – CN, CHN, China
158 – TW, TWN, Taiwan Province of China
162 – CX, CXR, Christmas Island
166 – CC, CCK, Cocos (Keeling) Islands
170 – CO, COL, Colombia
174 – KM, COM, Comoros
175 – YT, MYT, Mayotte
178 – CG, COG, Congo
180 – CD, COD, Congo
184 – CK, COK, Cook Islands
188 – CR, CRI, Costa Rica
191 – HR, HRV, Croatia
192 – CU, CUB, Cuba
196 – CY, CYP, Cyprus
203 – CZ, CZE, Czech Republic
204 – BJ, BEN, Benin
208 – DK, DNK, Denmark
212 – DM, DMA, Dominica
214 – DO, DOM, Dominican Republic
218 – EC, ECU, Ecuador
222 – SV, SLV, El Salvador
226 – GQ, GNQ, Equatorial Guinea
231 – ET, ETH, Ethiopia
232 – ER, ERI, Eritrea
233 – EE, EST, Estonia
234 – FO, FRO, Faroe Islands
238 – FK, FLK, Falkland Islands (Malvinas)
239 – GS, SGS, South Georgia and the South Sandwich Islands
242 – FJ, FJI, Fiji
246 – FI, FIN, Finland
248 – AX, ALA, Aland Islands
250 – FR, FRA, France
254 – GF, GUF, French Guiana
258 – PF, PYF, French Polynesia
260 – TF, ATF, French Southern Territories
262 – DJ, DJI, Djibouti
266 – GA, GAB, Gabon
268 – GE, GEO, Georgia
270 – GM, GMB, Gambia
275 – PS, PSE, Palestinian Territory Occupied
276 – DE, DEU, Germany
288 – GH, GHA, Ghana
292 – GI, GIB, Gibraltar
296 – KI, KIR, Kiribati
300 – GR, GRC, Greece
304 – GL, GRL, Greenland
308 – GD, GRD, Grenada
312 – GP, GLP, Guadeloupe
316 – GU, GUM, Guam
320 – GT, GTM, Guatemala
324 – GN, GIN, Guinea
328 – GY, GUY, Guyana
332 – HT, HTI, Haiti
334 – HM, HMD, Heard Island and McDonald Islands
336 – VA, VAT, Holy See (Vatican City State)
340 – HN, HND, Honduras
344 – HK, HKG, Hong Kong
348 – HU, HUN, Hungary
352 – IS, ISL, Iceland
356 – IN, IND, India
360 – ID, IDN, Indonesia
364 – IR, IRN, Iran
368 – IQ, IRQ, Iraq
372 – IE, IRL, Ireland
376 – IL, ISR, Israel
380 – IT, ITA, Italy
384 – CI, CIV, Ivory Coast
388 – JM, JAM, Jamaica
392 – JP, JPN, Japan
398 – KZ, KAZ, Kazakhstan
400 – JO, JOR, Jordan
404 – KE, KEN, Kenya
408 – KP, PRK, North Korea
410 – KR, KOR, South Korea
414 – KW, KWT, Kuwait
417 – KG, KGZ, Kyrgyzstan
418 – LA, LAO, Lao People’s Democratic Republic
422 – LB, LBN, Lebanon
426 – LS, LSO, Lesotho
428 – LV, LVA, Latvia
430 – LR, LBR, Liberia
434 – LY, LBY, Libya
438 – LI, LIE, Liechtenstein
440 – LT, LTU, Lithuania
442 – LU, LUX, Luxembourg
446 – MO, MAC, Macao
450 – MG, MDG, Madagascar
454 – MW, MWI, Malawi
458 – MY, MYS, Malaysia
462 – MV, MDV, Maldives
466 – ML, MLI, Mali
470 – MT, MLT, Malta
474 – MQ, MTQ, Martinique
478 – MR, MRT, Mauritania
480 – MU, MUS, Mauritius
484 – MX, MEX, Mexico
492 – MC, MCO, Monaco
496 – MN, MNG, Mongolia
498 – MD, MDA, Moldova
499 – ME, MNE, Montenegro
500 – MS, MSR, Montserrat
504 – MA, MAR, Morocco
508 – MZ, MOZ, Mozambique
512 – OM, OMN, Oman
516 – NA, NAM, Namibia
520 – NR, NRU, Nauru
524 – NP, NPL, Nepal
528 – NL, NLD, Netherlands
531 – CW, CUW, Curacao
533 – AW, ABW, Aruba
534 – SX, SXM, Sint Maarten (Dutch part)
535 – BQ, BES, Bonaire Sint Eustatius and Saba
540 – NC, NCL, New Caledonia
548 – VU, VUT, Vanuatu
554 – NZ, NZL, New Zealand
558 – NI, NIC, Nicaragua
562 – NE, NER, Niger
566 – NG, NGA, Nigeria
570 – NU, NIU, Niue
574 – NF, NFK, Norfolk Island
578 – NO, NOR, Norway
580 – MP, MNP, Northern Mariana Islands
581 – UM, UMI, United States Minor Outlying Islands
583 – FM, FSM, Micronesia
584 – MH, MHL, Marshall Islands
585 – PW, PLW, Palau
586 – PK, PAK, Pakistan
591 – PA, PAN, Panama
598 – PG, PNG, Papua New Guinea
600 – PY, PRY, Paraguay
604 – PE, PER, Peru
608 – PH, PHL, Philippines
612 – PN, PCN, Pitcairn
616 – PL, POL, Poland
620 – PT, PRT, Portugal
624 – GW, GNB, Guinea-Bissau
626 – TL, TLS, Timor-Leste
630 – PR, PRI, Puerto Rico
634 – QA, QAT, Qatar
638 – RE, REU, Reunion
642 – RO, ROU, Romania
643 – RU, RUS, Russian Federation
646 – RW, RWA, Rwanda
652 – BL, BLM, Saint Barthelemy
654 – SH, SHN, Saint Helena Ascension and Tristan da Cunha
659 – KN, KNA, Saint Kitts and Nevis
660 – AI, AIA, Anguilla
662 – LC, LCA, Saint Lucia
663 – MF, MAF, Saint Martin (French part)
666 – PM, SPM, Saint Pierre and Miquelon
670 – VC, VCT, Saint Vincent and the Grenadines
674 – SM, SMR, San Marino
678 – ST, STP, Sao Tome and Principe
682 – SA, SAU, Saudi Arabia
686 – SN, SEN, Senegal
688 – RS, SRB, Serbia
690 – SC, SYC, Seychelles
694 – SL, SLE, Sierra Leone
702 – SG, SGP, Singapore
703 – SK, SVK, Slovakia
704 – VN, VNM, Viet Nam
705 – SI, SVN, Slovenia
706 – SO, SOM, Somalia
710 – ZA, ZAF, South Africa
716 – ZW, ZWE, Zimbabwe
724 – ES, ESP, Spain
728 – SS, SSD, South Sudan
729 – SD, SDN, Sudan
732 – EH, ESH, Western Sahara
740 – SR, SUR, Suriname
744 – SJ, SJM, Svalbard and Jan Mayen
748 – SZ, SWZ, Swaziland
752 – SE, SWE, Sweden
756 – CH, CHE, Switzerland
760 – SY, SYR, Syrian Arab Republic
762 – TJ, TJK, Tajikistan
764 – TH, THA, Thailand
768 – TG, TGO, Togo
772 – TK, TKL, Tokelau
776 – TO, TON, Tonga
780 – TT, TTO, Trinidad and Tobago
784 – AE, ARE, United Arab Emirates
788 – TN, TUN, Tunisia
792 – TR, TUR, Turkey
795 – TM, TKM, Turkmenistan
796 – TC, TCA, Turks and Caicos Islands
798 – TV, TUV, Tuvalu
800 – UG, UGA, Uganda
804 – UA, UKR, Ukraine
807 – MK, MKD, Macedonia
818 – EG, EGY, Egypt
826 – GB, GBR, United Kingdom
831 – GG, GGY, Guernsey
832 – JE, JEY, Jersey
833 – IM, IMN, Isle of Man
834 – TZ, TZA, Tanzania
840 – US, USA, United States
850 – VI, VIR, Virgin Islands U.S.
854 – BF, BFA, Burkina Faso
858 – UY, URY, Uruguay
860 – UZ, UZB, Uzbekistan
862 – VE, VEN, Venezuela
876 – WF, WLF, Wallis and Futuna
882 – WS, WSM, Samoa
887 – YE, YEM, Yemen
894 – ZM, ZMB, Zambia

Tags: , , ,