The best way to store country, name, and continent codes in Java

I want to have a List or Array sorts, storing this information about each country:

  • 2 letter code
  • Country name e.g. Brazil
  • Continent / region of the world such as Eastern Europe, North America, etc.

I will classify each country in the region / continent manually (but if there is a way to do this automatically, let me know). This question is about how to store and access countries. For example, I want to get all the countries of North America.

I do not want to use local text files or such, because this project will be converted to javascript using the Google Web Toolkit. But storing some kind of resource in Enum or another file, keeping it separate from the rest of the code, is what I really really am.

+7
java linked-list arrays hashmap treemap
source share
5 answers

There are 246 countries in ISO 3166, you can receive a large amount of relay in response to this. I prefer to use an XML file with a list of countries, you can download it from http://www.iso.org/ and download them (for example, when the application starts). Then, since you need them in GWT, load them back as an RPC call, but don't forget to cache them (some kind of lazy load) so that you don't finish loading them every time. I think that this will be better in any case than containing them in the code, since you will finish loading the full list every time you access the module, even if the user does not need to use this list.

So, you need something that will hold the country:

 public class Country { private final String name; private final String code; public Country(String name, String code) { this.name = name; this.code = code; } public String getName() { return name; } public String getCode() { return code; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Country country = (Country) obj; return code.equals(country.code); } public int hashCode() { return code.hashCode(); } } 

For GWT, this class will need to implement IsSerializable. And you can download them server side using:

 import java.util.ArrayList; import java.util.List; import java.io.InputStream; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class CountriesService { private static final String EL_COUNTRY = "ISO_3166-1_Entry"; private static final String EL_COUNTRY_NAME = "ISO_3166-1_Country_name"; private static final String EL_COUNTRY_CODE = "ISO_3166-1_Alpha-2_Code_element"; private List<Country> countries = new ArrayList<Country>(); public CountriesService(InputStream countriesList) { parseCountriesList(countriesList); } public List<Country> getCountries() { return countries; } private void parseCountriesList(InputStream countriesList) { countries.clear(); try { Document document = parse(countriesList); Element root = document.getRootElement(); //noinspection unchecked Iterator<Element> i = root.elementIterator(EL_COUNTRY); while (i.hasNext()) { Element countryElement = i.next(); Element countryName = countryElement.element(EL_COUNTRY_NAME); Element countryCode = countryElement.element(EL_COUNTRY_CODE); String countryname = countryName.getText(); countries.add(new Country(countryname, countryCode.getText())); } } catch (DocumentException e) { log.error(e, "Cannot read countries list"); } catch (IOException e) { log.error(e, "Cannot read countries list"); } } public static Document parse(InputStream inputStream) throws DocumentException { SAXReader reader = new SAXReader(); return reader.read(inputStream); } } 

Of course, if you need to find a country using the ISO 2 letter code, you probably want to change the list to a map. If, as you mentioned, you need individual countries by continent, you can extend the XML from ISO 3166 and add your own elements. Just check them out (ISO site).

+5
source share

Just enter enum called Country. Java enumerations can have properties, so your country code and name are there. For the continent you want to get another listing.

 public enum Continent { AFRICA, ANTARCTICA, ASIA, AUSTRALIA, EUROPE, NORTH_AMERICA, SOUTH_AMERICA } public enum Country { ALBANIA("AL", "Albania", Continent.EUROPE), ANDORRA("AN", "Andorra", Continent.EUROPE), ... private String code; private String name; private Continent continent; // get methods go here private Country(String code, String name, Continent continent) { this.code = code; this.name = name; this.continent = continent; } } 

As for saving and access, one of the standard solutions will be one Map for each of the fields that you will look for, taking into account this field. Since you have several values ​​for the continent, you will either have to use Map<?, List<Country>> , or an implementation of Multimap, for example. from the total amount of Apache.

+13
source share

If you often need to search by continent, I would simply make a series of immutable lists, one for each continent, and fill them out accordingly. The list of country data for the continent will probably not change often enough so that the costs of restoring such an array are rebuilt when something needs to be changed.

In addition, if you want to manually classify a continent country, the rest is automatic and can be done programmatically.

+4
source share

The easiest way to do this is to create a country / continent structure in Java using a map (or any collection) and then save it using XStream

This will create an XML representation of the collection, and you can easily read than into your process and convert it back to the same type of collection that you originally created. Also, since it is XML, you can easily edit it outside of the code. those. just in a text editor.

See the XStream tutorial for more details.

+2
source share

using a Locale object is the best solution I can think of, you don't need to store anything. But if you use GWT, try to do it on the server side, as some say that you will not work correctly on the client side (since it will be switched to javascript), you can send them to the client in RPC, as indicated earlier.

0
source share

All Articles