Java Map from Array toMap(Object... pairs)

Here you can find the source of toMap(Object... pairs)

Description

to Map

License

Apache License

Declaration

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

Method Source Code


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

import java.util.*;

public class Main {
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> toMap(Object... pairs) {
        Map<K, V> ret = new HashMap<K, V>();
        if (pairs == null || pairs.length == 0)
            return ret;

        if (pairs.length % 2 != 0) {
            throw new IllegalArgumentException("Map pairs can not be odd number.");
        }//from  w  ww  .  j a  v  a2s  .com
        int len = pairs.length / 2;
        for (int i = 0; i < len; i++) {
            ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);
        }
        return ret;
    }
}

Related

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