Standardized US States and Canadian Provinces Java Map

别等时光非礼了梦想. 提交于 2021-01-07 01:36:35

问题


I need a Java dictionary to look up full state and province names from North American state and province abbreviations. For example, "TX" --> "Texas", "NS" --> "Nova Scotia".

I found the C# versions here: Standardized US States Array and Countries Array

What is the Java version using the Map collection?


回答1:


Here is the Java dictionary, mapping US state and Canadian province abbreviations to full names, adapted from the C# versions mentioned in the question. It uses static initialization for brevity.

// the US State and Canada abbreviation dictionary, abbreviation --> full name
private final static Map<String, String>  stateAbbreviationDictionary = new HashMap<>();

static {
  // USA
  stateAbbreviationDictionary.put("AL", "Alabama");
  stateAbbreviationDictionary.put("AK", "Alaska");
  stateAbbreviationDictionary.put("AZ", "Arizona");
  stateAbbreviationDictionary.put("AR", "Arkansas");
  stateAbbreviationDictionary.put("CA", "California");
  stateAbbreviationDictionary.put("CO", "Colorado");
  stateAbbreviationDictionary.put("CT", "Connecticut");
  stateAbbreviationDictionary.put("DE", "Delaware");
  stateAbbreviationDictionary.put("DC", "District Of Columbia");
  stateAbbreviationDictionary.put("FL", "Florida");
  stateAbbreviationDictionary.put("GA", "Georgia");
  stateAbbreviationDictionary.put("HI", "Hawaii");
  stateAbbreviationDictionary.put("ID", "Idaho");
  stateAbbreviationDictionary.put("IL", "Illinois");
  stateAbbreviationDictionary.put("IN", "Indiana");
  stateAbbreviationDictionary.put("IA", "Iowa");
  stateAbbreviationDictionary.put("KS", "Kansas");
  stateAbbreviationDictionary.put("KY", "Kentucky");
  stateAbbreviationDictionary.put("LA", "Louisiana");
  stateAbbreviationDictionary.put("ME", "Maine");
  stateAbbreviationDictionary.put("MD", "Maryland");
  stateAbbreviationDictionary.put("MA", "Massachusetts");
  stateAbbreviationDictionary.put("MI", "Michigan");
  stateAbbreviationDictionary.put("MN", "Minnesota");
  stateAbbreviationDictionary.put("MS", "Mississippi");
  stateAbbreviationDictionary.put("MO", "Missouri");
  stateAbbreviationDictionary.put("MT", "Montana");
  stateAbbreviationDictionary.put("NE", "Nebraska");
  stateAbbreviationDictionary.put("NV", "Nevada");
  stateAbbreviationDictionary.put("NH", "New Hampshire");
  stateAbbreviationDictionary.put("NJ", "New Jersey");
  stateAbbreviationDictionary.put("NM", "New Mexico");
  stateAbbreviationDictionary.put("NY", "New York");
  stateAbbreviationDictionary.put("NC", "North Carolina");
  stateAbbreviationDictionary.put("ND", "North Dakota");
  stateAbbreviationDictionary.put("OH", "Ohio");
  stateAbbreviationDictionary.put("OK", "Oklahoma");
  stateAbbreviationDictionary.put("OR", "Oregon");
  stateAbbreviationDictionary.put("PA", "Pennsylvania");
  stateAbbreviationDictionary.put("RI", "Rhode Island");
  stateAbbreviationDictionary.put("SC", "South Carolina");
  stateAbbreviationDictionary.put("SD", "South Dakota");
  stateAbbreviationDictionary.put("TN", "Tennessee");
  stateAbbreviationDictionary.put("TX", "Texas");
  stateAbbreviationDictionary.put("UT", "Utah");
  stateAbbreviationDictionary.put("VT", "Vermont");
  stateAbbreviationDictionary.put("VA", "Virginia");
  stateAbbreviationDictionary.put("WA", "Washington");
  stateAbbreviationDictionary.put("WV", "West Virginia");
  stateAbbreviationDictionary.put("WI", "Wisconsin");
  stateAbbreviationDictionary.put("WY", "Wyoming");
  // Canada
  stateAbbreviationDictionary.put("AB", "Alberta");
  stateAbbreviationDictionary.put("BC", "British Columbia");
  stateAbbreviationDictionary.put("MB", "Manitoba");
  stateAbbreviationDictionary.put("NB", "New Brunswick");
  stateAbbreviationDictionary.put("NL", "Newfoundland and Labrador");
  stateAbbreviationDictionary.put("NS", "Nova Scotia");
  stateAbbreviationDictionary.put("NT", "Northwest Territories");
  stateAbbreviationDictionary.put("NU", "Nunavut");
  stateAbbreviationDictionary.put("ON", "Ontario");
  stateAbbreviationDictionary.put("PE", "Prince Edward Island");
  stateAbbreviationDictionary.put("QC", "Quebec");
  stateAbbreviationDictionary.put("SK", "Saskatchewan");
  stateAbbreviationDictionary.put("YT", "Yukon");
}

Java 9 introduced immutable maps and here is the same dictionary implemented as an immutable map:

// the US State and Canada abbreviation dictionary, abbreviation --> full name
  private final static Map<String, String> stateAbbreviationDictionary = Map.ofEntries(
          // USA
          entry("AL", "Alabama"),
          entry("AK", "Alaska"),
          entry("AZ", "Arizona"),
          entry("AR", "Arkansas"),
          entry("CA", "California"),
          entry("CO", "Colorado"),
          entry("CT", "Connecticut"),
          entry("DE", "Delaware"),
          entry("DC", "District Of Columbia"),
          entry("FL", "Florida"),
          entry("GA", "Georgia"),
          entry("HI", "Hawaii"),
          entry("ID", "Idaho"),
          entry("IL", "Illinois"),
          entry("IN", "Indiana"),
          entry("IA", "Iowa"),
          entry("KS", "Kansas"),
          entry("KY", "Kentucky"),
          entry("LA", "Louisiana"),
          entry("ME", "Maine"),
          entry("MD", "Maryland"),
          entry("MA", "Massachusetts"),
          entry("MI", "Michigan"),
          entry("MN", "Minnesota"),
          entry("MS", "Mississippi"),
          entry("MO", "Missouri"),
          entry("MT", "Montana"),
          entry("NE", "Nebraska"),
          entry("NV", "Nevada"),
          entry("NH", "New Hampshire"),
          entry("NJ", "New Jersey"),
          entry("NM", "New Mexico"),
          entry("NY", "New York"),
          entry("NC", "North Carolina"),
          entry("ND", "North Dakota"),
          entry("OH", "Ohio"),
          entry("OK", "Oklahoma"),
          entry("OR", "Oregon"),
          entry("PA", "Pennsylvania"),
          entry("RI", "Rhode Island"),
          entry("SC", "South Carolina"),
          entry("SD", "South Dakota"),
          entry("TN", "Tennessee"),
          entry("TX", "Texas"),
          entry("UT", "Utah"),
          entry("VT", "Vermont"),
          entry("VA", "Virginia"),
          entry("WA", "Washington"),
          entry("WV", "West Virginia"),
          entry("WI", "Wisconsin"),
          entry("WY", "Wyoming"),
          // Canada
          entry("AB", "Alberta"),
          entry("BC", "British Columbia"),
          entry("MB", "Manitoba"),
          entry("NB", "New Brunswick"),
          entry("NL", "Newfoundland and Labrador"),
          entry("NS", "Nova Scotia"),
          entry("NT", "Northwest Territories"),
          entry("NU", "Nunavut"),
          entry("ON", "Ontario"),
          entry("PE", "Prince Edward Island"),
          entry("QC", "Quebec"),
          entry("SK", "Saskatchewan"),
          entry("YT", "Yukon"));


来源:https://stackoverflow.com/questions/65569851/standardized-us-states-and-canadian-provinces-java-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!