Java Map from Array map(String... strings)

Here you can find the source of map(String... strings)

Description

Creates a Map out of an array with Strings.

License

Open Source License

Parameter

Parameter Description
strings input strings, key-value alternating

Return

a parameter map

Declaration

public static Map<String, String> map(String... strings) 

Method Source Code

//package com.java2s;

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**//from  w  w w  .  j av a 2 s .  c o m
     * Creates a Map out of an array with Strings.
     *
     * @param strings input strings, key-value alternating
     * @return a parameter map
     */
    public static Map<String, String> map(String... strings) {
        if (strings.length % 2 != 0)
            throw new IllegalArgumentException("strings.length % 2 != 0");
        Map<String, String> mp = new HashMap<String, String>();
        for (int i = 0; i < strings.length; i += 2) {
            mp.put(strings[i], strings[i + 1]);
        }
        return mp;
    }
}

Related

  1. map(String key, String val)
  2. map(String... args)
  3. map(String... args)
  4. map(String... keysAndValues)
  5. map(String... keyValues)
  6. map(String... vals)
  7. map(T... input)
  8. toMap(Object... args)
  9. toMap(Object... args)