Java Utililty Methods Map from Array

List of utility methods to do Map from Array

Description

The list of methods to do Map from Array are organized into topic(s).

Method

Mapmap(String k, Object v)
map
Map<String, Object> map = new HashMap<String, Object>(1);
map.put(k, v);
return map;
Mapmap(String key, String val)
Create a single entry map
Map<String, String> map = new HashMap<String, String>(1);
map.put(key, val);
return map;
HashMapmap(String... args)
map
return map((Object[]) args);
Mapmap(String... args)
map
if (args == null || args.length == 0) {
    return Collections.emptyMap();
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < args.length;) {
    map.put(args[i++], args[i++]);
return map;
...
Mapmap(String... keysAndValues)
map
if (keysAndValues.length % 2 != 0) {
    throw new IllegalArgumentException("Argument count must be even.");
Map<String, String> map = new LinkedHashMap<>();
for (int i = 0; i < keysAndValues.length; i += 2) {
    map.put(keysAndValues[i], keysAndValues[i + 1]);
return unmodifiableMap(map);
...
Mapmap(String... keyValues)
map
HashMap<String, String> map = new HashMap<String, String>(keyValues.length / 2);
for (int i = 0; i < keyValues.length;) {
    map.put(keyValues[i++], keyValues[i++]);
return map;
Mapmap(String... strings)
Creates a Map out of an array with Strings.
if (strings.length % 2 != 0)
    throw new IllegalArgumentException("strings.length % 2 != 0");
Map<String, String> mp = new HashMap<String, String>();
for (int i = 0; i < strings.length; i += 2) {
    mp.put(strings[i], strings[i + 1]);
return mp;
Mapmap(String... vals)
map
HashMap<String, String> map = new HashMap<String, String>();
String key = null;
for (int i = 0; i < vals.length; i++) {
    String val = vals[i];
    if (key != null) {
        map.put(key, val);
        key = null;
    } else {
...
Mapmap(T... input)
Use import static org.couch4j.util.CollectionUtils.set;
 Map m = map("key1", "value1", "key2", "value2"); m => {key1: value1, key2: value2} 
if (null == input || input.length < 1) {
    return Collections.emptyMap();
if ((input.length & 1) == 1) {
    throw new IllegalArgumentException(
            "Input has to contain an even number of arguments: 'key1', 'value1', 'key2', 'values'...");
HashMap<T, T> m = new HashMap<T, T>();
...
MaptoMap(Object... args)
to Map
Map<String, Object> map = new HashMap<String, Object>();
int cntr = 0;
String name = null;
for (Object o : args) {
    if (cntr % 2 == 0) {
        name = (String) o;
    } else {
        map.put(name, o);
...