Java Map from Array map(T... input)

Here you can find the source of map(T... input)

Description

Use import static org.couch4j.util.CollectionUtils.set;
 Map m = map("key1", "value1", "key2", "value2"); m => {key1: value1, key2: value2} 

License

Open Source License

Parameter

Parameter Description
T a parameter

Return

Map based on the input arguments.

Declaration

public static <T> Map<T, T> map(T... input) 

Method Source Code


//package com.java2s;
/*/*ww  w .  j  a v a 2 s  .co  m*/
 * The MIT License
 *
 * Copyright (c) 2009, 2010 Stefan Saasen
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

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

import java.util.Iterator;

import java.util.Map;

public class Main {
    /**
     * Use import static org.couch4j.util.CollectionUtils.set;
     * 
     * <pre>
     * Map<String,String> m = map("key1", "value1", "key2", "value2");
     * 
     * m => {key1: value1, key2: value2}
     * </pre>
     * 
     * @param <T>
     * @param input, even number of elements. Elements should be key => value pairs.
     * @return Map based on the input arguments.
     */
    public static <T> Map<T, T> map(T... input) {
        if (null == input || input.length < 1) {
            return Collections.emptyMap();
        }
        if ((input.length & 1) == 1) {
            throw new IllegalArgumentException(
                    "Input has to contain an even number of arguments: 'key1', 'value1', 'key2', 'values'...");
        }
        HashMap<T, T> m = new HashMap<T, T>();
        for (Iterator<T> iterator = Arrays.asList(input).iterator(); iterator.hasNext();) {
            T key = iterator.next();
            T value = iterator.next();
            m.put(key, value);
        }
        return m;
    }
}

Related

  1. map(String... args)
  2. map(String... keysAndValues)
  3. map(String... keyValues)
  4. map(String... strings)
  5. map(String... vals)
  6. toMap(Object... args)
  7. toMap(Object... args)
  8. toMap(Object... data)
  9. toMap(Object... objects)