Java Map from Array toMap(Object[] array, Map map)

Here you can find the source of toMap(Object[] array, Map map)

Description

to Map

License

Open Source License

Declaration

public static Map toMap(Object[] array, Map map) 

Method Source Code

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

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static Map toMap(Object[] array) {
        return toMap(array, (Map) null);
    }//from  w w w.  j  av  a  2  s.  c  om

    public static Map toMap(Object[] array, Map map) {
        if (array == null) {
            return (Map) map;
        } else {
            if (map == null) {
                map = new HashMap((int) ((double) array.length * 1.5D));
            }

            for (int i = 0; i < array.length; ++i) {
                Object object = array[i];
                if (object instanceof Entry) {
                    Entry entry = (Entry) object;
                    ((Map) map).put(entry.getKey(), entry.getValue());
                } else {
                    if (!(object instanceof Object[])) {
                        throw new IllegalArgumentException("Array element " + i + ", '" + object
                                + "', is neither of type Map.Entry nor an Array");
                    }

                    Object[] entry = (Object[]) ((Object[]) object);
                    if (entry.length < 2) {
                        throw new IllegalArgumentException(
                                "Array element " + i + ", '" + object + "', has a length less than 2");
                    }

                    ((Map) map).put(entry[0], entry[1]);
                }
            }

            return (Map) map;
        }
    }
}

Related

  1. toMap(Object... objects)
  2. toMap(Object... pairs)
  3. toMap(Object... pairs)
  4. toMap(Object[] array)
  5. toMap(Object[] array)
  6. toMap(Object[] keys, Object[] values)
  7. toMap(Object[][] array)