开发者

Adding values in to a HashMap

开发者 https://www.devze.com 2023-03-31 06:22 出处:网络
I need help to add the values from a String array into a HashMap. if (!loaded){ synchronized(syncLock){

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

精彩评论

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