完全な州名を公式の州住所コードに変換する必要があります。たとえば、String New York から、NY をプロデュースする必要があります。これですべてをハッシュマップに入れることができましたが、これにはユーティリティが必要だと思われますか? 私はAndroid環境にいます。ただし、この 1 つのユーティリティのためだけに大きなライブラリをインポートしたくありません。ありがとう
質問する
24673 次
9 に答える
92
私はそれを既製にしました-あなたはそれを使うことができます:
Map<String, String> states = new HashMap<String, String>();
states.put("Alabama","AL");
states.put("Alaska","AK");
states.put("Alberta","AB");
states.put("American Samoa","AS");
states.put("Arizona","AZ");
states.put("Arkansas","AR");
states.put("Armed Forces (AE)","AE");
states.put("Armed Forces Americas","AA");
states.put("Armed Forces Pacific","AP");
states.put("British Columbia","BC");
states.put("California","CA");
states.put("Colorado","CO");
states.put("Connecticut","CT");
states.put("Delaware","DE");
states.put("District Of Columbia","DC");
states.put("Florida","FL");
states.put("Georgia","GA");
states.put("Guam","GU");
states.put("Hawaii","HI");
states.put("Idaho","ID");
states.put("Illinois","IL");
states.put("Indiana","IN");
states.put("Iowa","IA");
states.put("Kansas","KS");
states.put("Kentucky","KY");
states.put("Louisiana","LA");
states.put("Maine","ME");
states.put("Manitoba","MB");
states.put("Maryland","MD");
states.put("Massachusetts","MA");
states.put("Michigan","MI");
states.put("Minnesota","MN");
states.put("Mississippi","MS");
states.put("Missouri","MO");
states.put("Montana","MT");
states.put("Nebraska","NE");
states.put("Nevada","NV");
states.put("New Brunswick","NB");
states.put("New Hampshire","NH");
states.put("New Jersey","NJ");
states.put("New Mexico","NM");
states.put("New York","NY");
states.put("Newfoundland","NF");
states.put("North Carolina","NC");
states.put("North Dakota","ND");
states.put("Northwest Territories","NT");
states.put("Nova Scotia","NS");
states.put("Nunavut","NU");
states.put("Ohio","OH");
states.put("Oklahoma","OK");
states.put("Ontario","ON");
states.put("Oregon","OR");
states.put("Pennsylvania","PA");
states.put("Prince Edward Island","PE");
states.put("Puerto Rico","PR");
states.put("Quebec","QC");
states.put("Rhode Island","RI");
states.put("Saskatchewan","SK");
states.put("South Carolina","SC");
states.put("South Dakota","SD");
states.put("Tennessee","TN");
states.put("Texas","TX");
states.put("Utah","UT");
states.put("Vermont","VT");
states.put("Virgin Islands","VI");
states.put("Virginia","VA");
states.put("Washington","WA");
states.put("West Virginia","WV");
states.put("Wisconsin","WI");
states.put("Wyoming","WY");
states.put("Yukon Territory","YT");
于 2012-06-12T23:24:03.913 に答える
37
これが質問に答えないことはわかっていますが、誰かが反対のことを必要とする場合に備えて、州/州の略語をキーとして使用し、名前を値として使用します。マップを再フォーマットするのに数分かかりました。また、受け入れられた回答のケベックの略語は間違っています。
public static final Map<String, String> STATE_MAP;
static {
STATE_MAP = new HashMap<String, String>();
STATE_MAP.put("AL", "Alabama");
STATE_MAP.put("AK", "Alaska");
STATE_MAP.put("AB", "Alberta");
STATE_MAP.put("AZ", "Arizona");
STATE_MAP.put("AR", "Arkansas");
STATE_MAP.put("BC", "British Columbia");
STATE_MAP.put("CA", "California");
STATE_MAP.put("CO", "Colorado");
STATE_MAP.put("CT", "Connecticut");
STATE_MAP.put("DE", "Delaware");
STATE_MAP.put("DC", "District Of Columbia");
STATE_MAP.put("FL", "Florida");
STATE_MAP.put("GA", "Georgia");
STATE_MAP.put("GU", "Guam");
STATE_MAP.put("HI", "Hawaii");
STATE_MAP.put("ID", "Idaho");
STATE_MAP.put("IL", "Illinois");
STATE_MAP.put("IN", "Indiana");
STATE_MAP.put("IA", "Iowa");
STATE_MAP.put("KS", "Kansas");
STATE_MAP.put("KY", "Kentucky");
STATE_MAP.put("LA", "Louisiana");
STATE_MAP.put("ME", "Maine");
STATE_MAP.put("MB", "Manitoba");
STATE_MAP.put("MD", "Maryland");
STATE_MAP.put("MA", "Massachusetts");
STATE_MAP.put("MI", "Michigan");
STATE_MAP.put("MN", "Minnesota");
STATE_MAP.put("MS", "Mississippi");
STATE_MAP.put("MO", "Missouri");
STATE_MAP.put("MT", "Montana");
STATE_MAP.put("NE", "Nebraska");
STATE_MAP.put("NV", "Nevada");
STATE_MAP.put("NB", "New Brunswick");
STATE_MAP.put("NH", "New Hampshire");
STATE_MAP.put("NJ", "New Jersey");
STATE_MAP.put("NM", "New Mexico");
STATE_MAP.put("NY", "New York");
STATE_MAP.put("NF", "Newfoundland");
STATE_MAP.put("NC", "North Carolina");
STATE_MAP.put("ND", "North Dakota");
STATE_MAP.put("NT", "Northwest Territories");
STATE_MAP.put("NS", "Nova Scotia");
STATE_MAP.put("NU", "Nunavut");
STATE_MAP.put("OH", "Ohio");
STATE_MAP.put("OK", "Oklahoma");
STATE_MAP.put("ON", "Ontario");
STATE_MAP.put("OR", "Oregon");
STATE_MAP.put("PA", "Pennsylvania");
STATE_MAP.put("PE", "Prince Edward Island");
STATE_MAP.put("PR", "Puerto Rico");
STATE_MAP.put("QC", "Quebec");
STATE_MAP.put("RI", "Rhode Island");
STATE_MAP.put("SK", "Saskatchewan");
STATE_MAP.put("SC", "South Carolina");
STATE_MAP.put("SD", "South Dakota");
STATE_MAP.put("TN", "Tennessee");
STATE_MAP.put("TX", "Texas");
STATE_MAP.put("UT", "Utah");
STATE_MAP.put("VT", "Vermont");
STATE_MAP.put("VI", "Virgin Islands");
STATE_MAP.put("VA", "Virginia");
STATE_MAP.put("WA", "Washington");
STATE_MAP.put("WV", "West Virginia");
STATE_MAP.put("WI", "Wisconsin");
STATE_MAP.put("WY", "Wyoming");
STATE_MAP.put("YT", "Yukon Territory");
}
于 2014-05-28T17:15:19.000 に答える
7
Enumを使用する方がさらに良いです。
import java.util.HashMap;
import java.util.Map;
public enum State {
ALABAMA("Alabama", "AL"), ALASKA("Alaska", "AK"), AMERICAN_SAMOA("American Samoa", "AS"), ARIZONA("Arizona", "AZ"), ARKANSAS(
"Arkansas", "AR"), CALIFORNIA("California", "CA"), COLORADO("Colorado", "CO"), CONNECTICUT("Connecticut", "CT"), DELAWARE(
"Delaware", "DE"), DISTRICT_OF_COLUMBIA("District of Columbia", "DC"), FEDERATED_STATES_OF_MICRONESIA(
"Federated States of Micronesia", "FM"), FLORIDA("Florida", "FL"), GEORGIA("Georgia", "GA"), GUAM("Guam", "GU"), HAWAII(
"Hawaii", "HI"), IDAHO("Idaho", "ID"), ILLINOIS("Illinois", "IL"), INDIANA("Indiana", "IN"), IOWA("Iowa", "IA"), KANSAS(
"Kansas", "KS"), KENTUCKY("Kentucky", "KY"), LOUISIANA("Louisiana", "LA"), MAINE("Maine", "ME"), MARYLAND("Maryland", "MD"), MARSHALL_ISLANDS(
"Marshall Islands", "MH"), MASSACHUSETTS("Massachusetts", "MA"), MICHIGAN("Michigan", "MI"), MINNESOTA("Minnesota", "MN"), MISSISSIPPI(
"Mississippi", "MS"), MISSOURI("Missouri", "MO"), MONTANA("Montana", "MT"), NEBRASKA("Nebraska", "NE"), NEVADA("Nevada",
"NV"), NEW_HAMPSHIRE("New Hampshire", "NH"), NEW_JERSEY("New Jersey", "NJ"), NEW_MEXICO("New Mexico", "NM"), NEW_YORK(
"New York", "NY"), NORTH_CAROLINA("North Carolina", "NC"), NORTH_DAKOTA("North Dakota", "ND"), NORTHERN_MARIANA_ISLANDS(
"Northern Mariana Islands", "MP"), OHIO("Ohio", "OH"), OKLAHOMA("Oklahoma", "OK"), OREGON("Oregon", "OR"), PALAU("Palau",
"PW"), PENNSYLVANIA("Pennsylvania", "PA"), PUERTO_RICO("Puerto Rico", "PR"), RHODE_ISLAND("Rhode Island", "RI"), SOUTH_CAROLINA(
"South Carolina", "SC"), SOUTH_DAKOTA("South Dakota", "SD"), TENNESSEE("Tennessee", "TN"), TEXAS("Texas", "TX"), UTAH(
"Utah", "UT"), VERMONT("Vermont", "VT"), VIRGIN_ISLANDS("Virgin Islands", "VI"), VIRGINIA("Virginia", "VA"), WASHINGTON(
"Washington", "WA"), WEST_VIRGINIA("West Virginia", "WV"), WISCONSIN("Wisconsin", "WI"), WYOMING("Wyoming", "WY"), UNKNOWN(
"Unknown", "");
/**
* The state's name.
*/
private String name;
/**
* The state's abbreviation.
*/
private String abbreviation;
/**
* The set of states addressed by abbreviations.
*/
private static final Map<String, State> STATES_BY_ABBR = new HashMap<String, State>();
/* static initializer */
static {
for (State state : values()) {
STATES_BY_ABBR.put(state.getAbbreviation(), state);
}
}
/**
* Constructs a new state.
*
* @param name the state's name.
* @param abbreviation the state's abbreviation.
*/
State(String name, String abbreviation) {
this.name = name;
this.abbreviation = abbreviation;
}
/**
* Returns the state's abbreviation.
*
* @return the state's abbreviation.
*/
public String getAbbreviation() {
return abbreviation;
}
/**
* Gets the enum constant with the specified abbreviation.
*
* @param abbr the state's abbreviation.
* @return the enum constant with the specified abbreviation.
* @throws SunlightException if the abbreviation is invalid.
*/
public static State valueOfAbbreviation(final String abbr) {
final State state = STATES_BY_ABBR.get(abbr);
if (state != null) {
return state;
} else {
return UNKNOWN;
}
}
public static State valueOfName(final String name) {
final String enumName = name.toUpperCase().replaceAll(" ", "_");
try {
return valueOf(enumName);
} catch (final IllegalArgumentException e) {
return State.UNKNOWN;
}
}
@Override
public String toString() {
return name;
}
}
于 2017-02-14T17:37:52.387 に答える
5
これは、米国 (US) および米国領、カナダ、およびメキシコのコードを含む更新版です。これは双方向マップです。そのため、これは指定されたコードの名前を提供し、その逆も同様です。下にスクロールして、両方のオプションを表示します。
private static final Map<String, String> stateCodeToNameMap = new HashMap<>();
private static final Map<String, String> stateNameToCodeMap = new HashMap<>();
static {
// Load US Codes.
stateCodeToNameMap.put("AK", "Alaska");
stateCodeToNameMap.put("AL", "Alabama");
stateCodeToNameMap.put("AR", "Arkansas");
stateCodeToNameMap.put("AZ", "Arizona");
stateCodeToNameMap.put("CA", "California");
stateCodeToNameMap.put("CO", "Colorado");
stateCodeToNameMap.put("CT", "Connecticut");
stateCodeToNameMap.put("DC", "District of Columbia");
stateCodeToNameMap.put("DE", "Delaware");
stateCodeToNameMap.put("FL", "Florida");
stateCodeToNameMap.put("GA", "Georgia");
stateCodeToNameMap.put("HI", "Hawaii");
stateCodeToNameMap.put("IA", "Iowa");
stateCodeToNameMap.put("ID", "Idaho");
stateCodeToNameMap.put("IL", "Illinois");
stateCodeToNameMap.put("IN", "Indiana");
stateCodeToNameMap.put("KS", "Kansas");
stateCodeToNameMap.put("KY", "Kentucky");
stateCodeToNameMap.put("LA", "Louisiana");
stateCodeToNameMap.put("MA", "Massachusetts");
stateCodeToNameMap.put("MD", "Maryland");
stateCodeToNameMap.put("ME", "Maine");
stateCodeToNameMap.put("MI", "Michigan");
stateCodeToNameMap.put("MN", "Minnesota");
stateCodeToNameMap.put("MO", "Missouri");
stateCodeToNameMap.put("MS", "Mississippi");
stateCodeToNameMap.put("MT", "Montana");
stateCodeToNameMap.put("NC", "North Carolina");
stateCodeToNameMap.put("ND", "North Dakota");
stateCodeToNameMap.put("NE", "Nebraska");
stateCodeToNameMap.put("NH", "New Hampshire");
stateCodeToNameMap.put("NJ", "New Jersey");
stateCodeToNameMap.put("NM", "New Mexico");
stateCodeToNameMap.put("NV", "Nevada");
stateCodeToNameMap.put("NY", "New York");
stateCodeToNameMap.put("OH", "Ohio");
stateCodeToNameMap.put("OK", "Oklahoma");
stateCodeToNameMap.put("OR", "Oregon");
stateCodeToNameMap.put("PA", "Pennsylvania");
stateCodeToNameMap.put("RI", "Rhode Island");
stateCodeToNameMap.put("SC", "South Carolina");
stateCodeToNameMap.put("SD", "South Dakota");
stateCodeToNameMap.put("TN", "Tennessee");
stateCodeToNameMap.put("TX", "Texas");
stateCodeToNameMap.put("UT", "Utah");
stateCodeToNameMap.put("VA", "Virginia");
stateCodeToNameMap.put("VT", "Vermont");
stateCodeToNameMap.put("WA", "Washington");
stateCodeToNameMap.put("WI", "Wisconsin");
stateCodeToNameMap.put("WV", "West Virginia");
stateCodeToNameMap.put("WY", "Wyoming");
stateCodeToNameMap.put("GU", "Guam");
stateCodeToNameMap.put("VI", "Virgin Islands");
stateCodeToNameMap.put("PR", "Puerto Rico");
stateCodeToNameMap.put("AE", "Armed forces - Europe");
stateCodeToNameMap.put("AA", "Armed forces - America");
stateCodeToNameMap.put("AP", "Armed forces - Pacific");
// Load Canada Codes.
stateCodeToNameMap.put("AB", "Alberta");
stateCodeToNameMap.put("BC", "British Columbia");
stateCodeToNameMap.put("MB", "Manitoba");
stateCodeToNameMap.put("NB", "New Brunswick");
stateCodeToNameMap.put("NL", "Newfoundland and Labrador");
stateCodeToNameMap.put("NT", "Northwest Territories");
stateCodeToNameMap.put("NS", "Nova Scotia");
stateCodeToNameMap.put("NU", "Nunavut");
stateCodeToNameMap.put("ON", "Ontario");
stateCodeToNameMap.put("PE", "Prince Edward Island");
stateCodeToNameMap.put("QC", "Quebec");
stateCodeToNameMap.put("SK", "Saskatchewan");
stateCodeToNameMap.put("YT", "Yukon");
// Load México Codes.
stateCodeToNameMap.put("AGU", "Aguascalientes");
stateCodeToNameMap.put("BCN", "Baja California");
stateCodeToNameMap.put("BCS", "Baja California Sur");
stateCodeToNameMap.put("CAM", "Campeche");
stateCodeToNameMap.put("CHP", "Chiapas");
stateCodeToNameMap.put("CHH", "Chihuahua");
stateCodeToNameMap.put("COA", "Coahuila");
stateCodeToNameMap.put("COL", "Colima");
stateCodeToNameMap.put("DIF", "Distrito Federal");
stateCodeToNameMap.put("DUR", "Durango");
stateCodeToNameMap.put("GUA", "Guanajuato");
stateCodeToNameMap.put("GRO", "Guerrero");
stateCodeToNameMap.put("HID", "Hidalgo");
stateCodeToNameMap.put("JAL", "Jalisco");
stateCodeToNameMap.put("MEX", "México");
stateCodeToNameMap.put("MIC", "Michoacán");
stateCodeToNameMap.put("MOR", "Morelos");
stateCodeToNameMap.put("NAY", "Nayarit");
stateCodeToNameMap.put("NLE", "Nuevo León");
stateCodeToNameMap.put("OAX", "Oaxaca");
stateCodeToNameMap.put("PUE", "Puebla");
stateCodeToNameMap.put("QUE", "Querétaro");
stateCodeToNameMap.put("ROO", "Quintana Roo");
stateCodeToNameMap.put("SLP", "San Luis Potosí");
stateCodeToNameMap.put("SIN", "Sinaloa");
stateCodeToNameMap.put("SON", "Sonora");
stateCodeToNameMap.put("TAB", "Tabasco");
stateCodeToNameMap.put("TAM", "Tamaulipas");
stateCodeToNameMap.put("TLA", "Tlaxcala");
stateCodeToNameMap.put("VER", "Veracruz");
stateCodeToNameMap.put("YUC", "Yucatán");
stateCodeToNameMap.put("ZAC", "Zacatecas");
// Load US State Names.
stateNameToCodeMap.put("Alabama","AL");
stateNameToCodeMap.put("Alaska","AK");
stateNameToCodeMap.put("Arizona","AZ");
stateNameToCodeMap.put("Arkansas","AR");
stateNameToCodeMap.put("California","CA");
stateNameToCodeMap.put("Colorado","CO");
stateNameToCodeMap.put("Connecticut","CT");
stateNameToCodeMap.put("Delaware","DE");
stateNameToCodeMap.put("District Of Columbia","DC");
stateNameToCodeMap.put("Florida","FL");
stateNameToCodeMap.put("Georgia","GA");
stateNameToCodeMap.put("Hawaii","HI");
stateNameToCodeMap.put("Idaho","ID");
stateNameToCodeMap.put("Illinois","IL");
stateNameToCodeMap.put("Indiana","IN");
stateNameToCodeMap.put("Iowa","IA");
stateNameToCodeMap.put("Kansas","KS");
stateNameToCodeMap.put("Kentucky","KY");
stateNameToCodeMap.put("Louisiana","LA");
stateNameToCodeMap.put("Maine","ME");
stateNameToCodeMap.put("Maryland","MD");
stateNameToCodeMap.put("Massachusetts","MA");
stateNameToCodeMap.put("Michigan","MI");
stateNameToCodeMap.put("Minnesota","MN");
stateNameToCodeMap.put("Mississippi","MS");
stateNameToCodeMap.put("Missouri","MO");
stateNameToCodeMap.put("Montana","MT");
stateNameToCodeMap.put("Nebraska","NE");
stateNameToCodeMap.put("Nevada","NV");
stateNameToCodeMap.put("New Hampshire","NH");
stateNameToCodeMap.put("New Jersey","NJ");
stateNameToCodeMap.put("New Mexico","NM");
stateNameToCodeMap.put("New York","NY");
stateNameToCodeMap.put("North Carolina","NC");
stateNameToCodeMap.put("North Dakota","ND");
stateNameToCodeMap.put("Ohio","OH");
stateNameToCodeMap.put("Oklahoma","OK");
stateNameToCodeMap.put("Oregon","OR");
stateNameToCodeMap.put("Pennsylvania","PA");
stateNameToCodeMap.put("Rhode Island","RI");
stateNameToCodeMap.put("South Carolina","SC");
stateNameToCodeMap.put("South Dakota","SD");
stateNameToCodeMap.put("Tennessee","TN");
stateNameToCodeMap.put("Texas","TX");
stateNameToCodeMap.put("Utah","UT");
stateNameToCodeMap.put("Vermont","VT");
stateNameToCodeMap.put("Virginia","VA");
stateNameToCodeMap.put("Washington","WA");
stateNameToCodeMap.put("West Virginia","WV");
stateNameToCodeMap.put("Wisconsin","WI");
stateNameToCodeMap.put("Wyoming","WY");
stateNameToCodeMap.put("Guam", "GU");
stateNameToCodeMap.put("Puerto Rico","PR");
stateNameToCodeMap.put("Virgin Islands","VI");
stateNameToCodeMap.put("Armed Forces (AE)","AE");
stateNameToCodeMap.put("Armed Forces Americas","AA");
stateNameToCodeMap.put("Armed Forces Pacific","AP");
// Load Canada State Names.
stateNameToCodeMap.put("Alberta","AB");
stateNameToCodeMap.put("British Columbia","BC");
stateNameToCodeMap.put("Manitoba","MB");
stateNameToCodeMap.put("New Brunswick","NB");
stateNameToCodeMap.put("Newfoundland and Labrador","NF");
stateNameToCodeMap.put("Northwest Territories","NT");
stateNameToCodeMap.put("Nova Scotia","NS");
stateNameToCodeMap.put("Nunavut","NU");
stateNameToCodeMap.put("Ontario","ON");
stateNameToCodeMap.put("Prince Edward Island","PE");
stateNameToCodeMap.put("Quebec","QC");
stateNameToCodeMap.put("Saskatchewan","SK");
stateNameToCodeMap.put("Yukon Territory","YT");
// Load México State Names.
stateNameToCodeMap.put("Aguascalientes", "AGU");
stateNameToCodeMap.put("Baja California", "BCN");
stateNameToCodeMap.put("Baja California Sur", "BCS");
stateNameToCodeMap.put("Campeche", "CAM");
stateNameToCodeMap.put("Chiapas", "CHP");
stateNameToCodeMap.put("Chihuahua", "CHH");
stateNameToCodeMap.put("Coahuila", "COA");
stateNameToCodeMap.put("Colima", "COL");
stateNameToCodeMap.put("Distrito Federal", "DIF");
stateNameToCodeMap.put("Durango", "DUR");
stateNameToCodeMap.put("Guanajuato", "GUA");
stateNameToCodeMap.put("Guerrero", "GRO");
stateNameToCodeMap.put("Hidalgo", "HID");
stateNameToCodeMap.put("Jalisco", "JAL");
stateNameToCodeMap.put("México", "MEX");
stateNameToCodeMap.put("Michoacán", "MIC");
stateNameToCodeMap.put("Morelos", "MOR");
stateNameToCodeMap.put("Nayarit", "NAY");
stateNameToCodeMap.put("Nuevo León", "NLE");
stateNameToCodeMap.put("Oaxaca", "OAX");
stateNameToCodeMap.put("Puebla", "PUE");
stateNameToCodeMap.put("Querétaro", "QUE");
stateNameToCodeMap.put("Quintana Roo", "ROO");
stateNameToCodeMap.put("San Luis Potosí", "SLP");
stateNameToCodeMap.put("Sinaloa", "SIN");
stateNameToCodeMap.put("Sonora", "SON");
stateNameToCodeMap.put("Tabasco", "TAB");
stateNameToCodeMap.put("Tamaulipas", "TAM");
stateNameToCodeMap.put("Tlaxcala", "TLA");
stateNameToCodeMap.put("Veracruz", "VER");
stateNameToCodeMap.put("Yucatán", "YUC");
stateNameToCodeMap.put("Zacatecas", "ZAC");
}
于 2016-09-13T21:50:38.833 に答える
1
コードと値を使用して構築しMap<String, String>
ます。これは簡単で、データはほとんどありません。これには「ユーティリティ」はありません。構築しているアプリケーションに固有のものです。
于 2012-06-12T22:29:26.357 に答える
1
最も簡単な方法は、HashMap を使用することだと思います。変換するライブラリがあったとしても、おそらく同じものを使用するでしょう。
Map<String, String> states = new HashMap<String, String>();
states.put("Arizona", "AZ");
states.put("California", "CA");
// So on and so forth...
// Then you could create a method like
public String toStateCode(String s) {
return states.get(s);
}
于 2012-06-12T22:33:29.773 に答える
0
私はそうは思いません(少なくとも私は聞いたことがありません)。しかし、いつでも辞書を使用してそれを達成できます。
于 2012-06-12T22:30:19.823 に答える