开发者

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜