Java Map from Array toMap(Object[] array)

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

Description

to Map

License

Open Source License

Declaration

public static Map toMap(Object[] array) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {

    public static Map toMap(Object[] array) {
        return toMap(array, null);
    }//from  w  w w. j a v  a  2  s  .  c o m

    public static Map toMap(Object[] array, Map map) {
        if (array == null) {
            return map;
        }

        if (map == null) {
            map = new HashMap((int) (array.length * 1.5));
        }

        for (int i = 0; i < array.length; i++) {
            Object object = array[i];

            if (object instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) object;

                map.put(entry.getKey(), entry.getValue());
            } else if (object instanceof Object[]) {
                Object[] entry = (Object[]) object;

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

                map.put(entry[0], entry[1]);
            } else {
                throw new IllegalArgumentException(
                        "Array element " + i + ", '" + object + "', is neither of type Map.Entry nor an Array");
            }
        }

        return map;
    }
}

Related

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