Best way to store Country codes, names, and Continent in Java

后端 未结 5 971
Happy的楠姐
Happy的楠姐 2021-02-03 13:30

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

  • 2 letter code
  • Country name such as Br
5条回答
  •  伪装坚强ぢ
    2021-02-03 14:02

    If you frequently need to do a lookup by continent, I'd simply make a series of immutable lists, one for each continent, and populate them accordingly. The list of country-data for a continent is probably not going to change frequently enough for the cost of rebuilding such an array to be rebuilt when something needs to be altered.

    Also, if you're willing to do the country-continent classification manually, the rest is automatic and can be done programmatically.

    • Use Locale.getISOCountries() to get the list of country codes.
    • Use Locale.getDisplayCountry() to get the display name of each country.

提交回复
热议问题