我需要一个java字典来查找完整的州和省的名称从北美州和省的缩写。例如,“德克萨斯”-->“德克萨斯”,“ns”-->“新斯科舍”。我在这里找到了c版本:标准化的美国州数组和国家数组使用Map集合的java版本是什么?
wz8daaqr1#
这是java字典,将美国各州和加拿大省的缩写Map为全名,改编自问题中提到的c版本。为了简洁起见,它使用静态初始化。
// 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引入了不可变Map,下面是实现为不可变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"));
1条答案
按热度按时间wz8daaqr1#
这是java字典,将美国各州和加拿大省的缩写Map为全名,改编自问题中提到的c版本。为了简洁起见,它使用静态初始化。
java 9引入了不可变Map,下面是实现为不可变Map的同一个字典: