user2434 user2434 - 3 months ago 21
Java Question

Converting string arrays into Map

I have two string arrays keys and values

String[] keys = {a,b,c,d};

String[] values = {1,2,3,4};


What is the fastest way to convert them into a map? I know we can iterate through them. But, is there any utility present?

Answer

Faster than this?

HashMap<String,String> map = new HashMap<String,String>();
if(keys.length == values.length){
    for(int index = 0; index < keys.length; index++){
        map.put(keys[index], values[index]);
    }
}