All Downloads are FREE. Search and download functionalities are using the official Maven repository.

chaschev.lang.Maps2 Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
package chaschev.lang;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Andrey Chaschev [email protected]
 */
public class Maps2 {
    public static  Map newHashMap(Object... keyValues) {
        return newHashMap(Maps.newHashMapWithExpectedSize(keyValues.length / 2), keyValues);
    }

    public static  Map newLinkedHashMap(Object... keyValues) {
        return newHashMap(Maps.newLinkedHashMap(), keyValues);
    }

    static  Map newHashMap(HashMap mapImpl, Object... keyValues){
        Preconditions.checkArgument(keyValues.length % 2 == 0, "parameters is a flat list of pairs");

        HashMap map = mapImpl;

        for (int i = 0; i < keyValues.length; i+=2) {
            Object key = keyValues[i];
            Object value = keyValues[i + 1];

            map.put(key, value);
        }

        return (Map) map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy