Here you can find the source of map(T... input)
Mapm = map("key1", "value1", "key2", "value2"); m => {key1: value1, key2: value2}
Parameter | Description |
---|---|
T | a parameter |
public static <T> Map<T, T> map(T... input)
//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; } }