Java Map Create createMap(Object... objects)

Here you can find the source of createMap(Object... objects)

Description

create Map

License

Open Source License

Declaration

public static <K, V> Map<K, V> createMap(Object... objects) 

Method Source Code


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

import java.util.*;

public class Main {
    public static <K, V> Map<K, V> createMap(Object... objects) {
        if (isEmpty(objects)) {
            return Collections.emptyMap();
        }/*www.  j  ava2s  .  c  o m*/

        if (objects.length % 2 != 0) {
            throw new IllegalArgumentException("objects should be even");
        }

        Map<K, V> map = new HashMap<>();

        for (int i = 0; i < objects.length; i++) {
            map.put((K) objects[i], (V) objects[++i]);
        }

        return map;
    }

    private static <E> boolean isEmpty(Collection<E> collection) {

        return (collection == null || collection.isEmpty());
    }

    private static <E> boolean isEmpty(E[] array) {

        return (array == null || array.length == 0);
    }
}

Related

  1. createMap(List keys, List values)
  2. createMap(Map map)
  3. createMap(Object keys[], Object values[])
  4. createMap(Object... args)
  5. createMap(Object... objects)
  6. createMap(Object[] keysValues)
  7. createMap(String entries)
  8. createMap(String key, Object value)
  9. createMap(String[] list1, String[] list2)

  10. HOME | Copyright © www.java2s.com 2016