Java Map Create createMap(String[] mappings, String sep)

Here you can find the source of createMap(String[] mappings, String sep)

Description

Create map from string description.

License

Open Source License

Parameter

Parameter Description
mappings List of mappings.
sep Separator used in each mapping.

Return

Map corresponding to the provided mapping.

Declaration

public static Map<String, String> createMap(String[] mappings, String sep) 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//from  ww w .j a va  2 s. co m
     * Create map from string description.
     * 
     * The input mapping describes single mapping in a single string, separated
     * by the provided separator.
     * 
     * On input <code>[ "a:alpha", "c:charlie" ]</code> the output would be
     * <code>{ "a" => "alpha", "c" => "charlie" }</code>.
     * 
     * @param mappings
     *            List of mappings.
     * @param sep
     *            Separator used in each mapping.
     * @return Map corresponding to the provided mapping.
     */
    public static Map<String, String> createMap(String[] mappings, String sep) {
        Map<String, String> dict = new HashMap<>();
        for (String m : mappings) {
            String[] parts = m.split(sep, 2);
            if (parts.length == 1) {
                dict.put(m, m);
            } else {
                dict.put(parts[0], parts[1]);
            }
        }
        return dict;
    }
}

Related

  1. createMap(Object... objects)
  2. createMap(Object[] keysValues)
  3. createMap(String entries)
  4. createMap(String key, Object value)
  5. createMap(String[] list1, String[] list2)
  6. createMap(T... _args)
  7. createMap(U... items)
  8. createMap_pollutantHourly()
  9. createMapFor(String... args)