Adding values in to a HashMap
I need help to add the values from a String array into a HashMap.
if (!loaded){
synchronized(syncLock){
if (!loaded){
loaded=true;
if (prefix!=null){
prefixMap = new HashMap<Integer, Float>();
String userDefaultPrefix[] = pref开发者_如何学运维ix.split("~");
}
}
}
}
I have the strings stored in userDefaultPrefix, and i need to add those values into prefixMap. TIA
If I get you right and you're sure in data quality than you can fill prefixMap following way:
for (int i = 0; i < userDefaultPrefix.length; i += 2) {
if (i+1 < userDefaultPrefix.length) {
prefixMap.put(Integer.parseInt(userDefaultPrefix[i]),
Float.parseFloat(userDefaultPrefix[i+1]));
}
}
assuming you want a map of (i->userDefaultPrefix[i]):
for (int i = 0; i < userDefaultPrefix.length;i++) {
prefixMap.put(i,userDefaultPrefix[i]); //note that the autoboxing automatically boxes your int to an Integer
}
精彩评论