Java Map Add arrayToMap(Object[] array)

Here you can find the source of arrayToMap(Object[] array)

Description

Convert an array to a HashMap, where the key and value of each map element is the same as each list element.

License

Apache License

Declaration

public static HashMap<Object, Object> arrayToMap(Object[] array) 

Method Source Code

//package com.java2s;
/*/*  w  w  w  .  j  a v  a 2 s  .c o  m*/
 * Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
 * Copyright [2016-2018] EMBL-European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

public class Main {
    /**
     * Convert an array to a HashMap, where the key and value of each map
     * element is the same as each list element.
     */
    public static HashMap<Object, Object> arrayToMap(Object[] array) {

        return listToMap(Arrays.asList(array));

    }

    /**
     * Convert a list to a HashMap, where the key and value of each map element
     * is the same as each list element.
     */
    public static HashMap<Object, Object> listToMap(List<Object> list) {

        HashMap<Object, Object> map = new HashMap<Object, Object>();
        for (Object o : list) {
            map.put(o, o);
        }

        return map;

    }
}

Related

  1. addToMapOfLists(Map> map, S key, T value)
  2. arrayAsMap(Object... t)
  3. arrayToMap(int[] array)
  4. arrayToMap(Map destMap, Object[]... oaaArray)
  5. ArrayToMap(Object[] a)
  6. arrayToMap(Object[] array, Object value)
  7. ArrayToMap(T[] list)
  8. arrayToMap(X[] objs)