Java Map from Array map(Object... objects)

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

Description

A short-hand method for creating a Map of key/value pairs where keys are String s and values are Object s.

License

Open Source License

Parameter

Parameter Description
objects alternating key and value.

Return

a Map with the entries supplied by objects .

Declaration

public static Map<String, Object> map(Object... objects) 

Method Source Code

//package com.java2s;
/**/*from ww  w  .j a va  2  s .c  om*/
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * A short-hand method for creating a {@link Map} of key/value pairs where
     * keys are {@link String}s and values are {@link Object}s.
     *
     * @param objects alternating key and value.
     * @return a Map with the entries supplied by {@code objects}.
     */
    public static Map<String, Object> map(Object... objects) {
        return genericMap(objects);
    }

    /**
     * A short-hand method for creating a {@link Map} of key/value pairs where
     * keys are {@link String}s and values are {@link Object}s.
     *
     * @param targetMap the {@link Map} to put the objects into.
     * @param objects alternating key and value.
     * @return a Map with the entries supplied by {@code objects}.
     */
    public static Map<String, Object> map(Map<String, Object> targetMap, Object... objects) {
        return genericMap(targetMap, objects);
    }

    /**
     * A short-hand method for creating a {@link Map} of key/value pairs.
     *
     * @param objects alternating key and value.
     * @param <K> type of keys
     * @param <V> type of values
     * @return a Map with the entries supplied by {@code objects}.
     */
    public static <K, V> Map<K, V> genericMap(Object... objects) {
        return genericMap(new HashMap<K, V>(), objects);
    }

    /**
     * A short-hand method for adding key/value pairs into a {@link Map}.
     *
     * @param targetMap the {@link Map} to put the objects into.
     * @param objects alternating key and value.
     * @param <K> type of keys
     * @param <V> type of values
     * @return a Map with the entries supplied by {@code objects}.
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> genericMap(Map<K, V> targetMap, Object... objects) {
        int i = 0;
        while (i < objects.length) {
            targetMap.put((K) objects[i++], (V) objects[i++]);
        }
        return targetMap;
    }
}

Related

  1. map(Object... elements)
  2. map(Object... keyvals)
  3. map(Object... mapValues)
  4. map(Object... objects)
  5. map(Object... objects)
  6. map(Object... objs)
  7. map(Object... objs)
  8. map(Object... oo)