Java Map from Array map(Object... keyvals)

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

Description

map

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> map(Object... keyvals) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> map(Object... keyvals) {
        if (keyvals == null) {
            return new HashMap<>();
        } else if (keyvals.length % 2 != 0) {
            throw new IllegalArgumentException(
                    "Map must have an even number of elements");
        } else {//from  www.j av a2 s. com
            Map<K, V> m = new HashMap<>(keyvals.length / 2);
            for (int i = 0; i < keyvals.length; i += 2) {
                m.put((K) keyvals[i], (V) keyvals[i + 1]);
            }
            return Collections.unmodifiableMap(m);
        }
    }
}

Related

  1. map(Object... elements)
  2. map(Object... mapValues)
  3. map(Object... objects)
  4. map(Object... objects)
  5. map(Object... objects)