Java Map Create map(Object... args)

Here you can find the source of map(Object... args)

Description

map

License

Open Source License

Declaration

public static <K, V> Map<K, V> map(Object... args) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static <K, V> Map<K, V> map(Object... args) {
        if (args.length % 2 != 0)
            throw new IllegalArgumentException("Length of arguments must be a multiple of 2");
        else if (args.length == 0)
            return Collections.emptyMap();
        Map<K, V> ret = new HashMap<>(args.length / 2);
        for (int idx = 0; idx < args.length; idx += 2) {
            //noinspection unchecked
            ret.put((K) args[idx], (V) args[idx + 1]);
        }/*ww  w . j av  a 2s. c  o m*/
        return ret;
    }
}

Related

  1. map(Map.Entry... entries)
  2. map(Map accumulator, Key key, Value value)
  3. map(Map outcomeMapping, String line, int firstTabIdx)
  4. map(Object key, Object value)
  5. map(Object obj)
  6. map(Object... data)
  7. map(Object... data)