Convert Array To Map - Java java.util

Java examples for java.util:Map Entry

Description

Convert Array To Map

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        Object objects = "java2s.com";
        System.out.println(listToMap(objects));
    }/*from  w w w. j  a  va  2  s .  c  om*/

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <K, V> Map<K, V> listToMap(Object... objects) {
        HashMap map = new HashMap();
        Object key = null;
        for (final Object object : objects) {
            if (key == null) {
                key = object;
            } else {
                map.put(key, object);
                key = null;
            }
        }
        return map;
    }
}

Related Tutorials