Java Map Create createMap(T... _args)

Here you can find the source of createMap(T... _args)

Description

Creates a map from the even-sized parameter array.

License

Open Source License

Parameter

Parameter Description
T map type
_args parameter array, any type

Return

map of parameter type

Declaration

@SafeVarargs
public static <T> Map<T, T> createMap(T... _args) 

Method Source Code

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

import java.util.Arrays;
import java.util.HashMap;

import java.util.Map;

public class Main {
    /**/*  w  ww . ja  v  a 2  s.co m*/
     * Creates a map from the even-sized parameter array.
     * @param <T> map type
     * @param _args parameter array, any type
     * @return map of parameter type
     */
    @SafeVarargs
    public static <T> Map<T, T> createMap(T... _args) {
        Map<T, T> map = new HashMap<>();
        if (_args != null) {
            if (_args.length % 2 != 0) {
                throw new IllegalArgumentException(
                        "Even number of parameters required to create map: " + Arrays.toString(_args));
            }
            for (int i = 0; i < _args.length;) {
                map.put(_args[i], _args[i + 1]);
                i += 2;
            }
        }
        return map;
    }
}

Related

  1. createMap(Object[] keysValues)
  2. createMap(String entries)
  3. createMap(String key, Object value)
  4. createMap(String[] list1, String[] list2)
  5. createMap(String[] mappings, String sep)
  6. createMap(U... items)
  7. createMap_pollutantHourly()
  8. createMapFor(String... args)
  9. createMapFromArgs(Object... args)