Java Map Create createMap(Object... objects)

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

Description

Create Map filled with values.

License

Apache License

Parameter

Parameter Description
objects Keys and values

Return

HashMap filled.

Declaration

@SuppressWarnings("unchecked")
public static <T, X> Map<T, X> createMap(Object... objects) 

Method Source Code

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

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

public class Main {
    /**//from w  ww.ja  v a 2  s .c  o m
     * Create Map filled with values. First value of array is key, second is the value, third is key...
     * 
     * @param objects
     *            Keys and values
     * @return HashMap filled.
     */
    @SuppressWarnings("unchecked")
    public static <T, X> Map<T, X> createMap(Object... objects) {
        Map<T, X> map = new HashMap<>();
        for (int i = 0; i < objects.length; i++) {
            map.put((T) objects[i], (X) objects[++i]);
        }
        return map;
    }
}

Related

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

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