Java String to Map toMap(String s)

Here you can find the source of toMap(String s)

Description

to Map

License

Apache License

Declaration

public static Map<String, String> toMap(String s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;

import java.util.Map;

public class Main {

    public static Map<String, String> toMap(String s) {
        Map<String, String> map = new HashMap<String, String>();
        if (isNull(s))
            return map;
        String[] ss = toArray(s, ";");
        for (int i = 0; i < ss.length; i++) {
            String[] value = toArray(ss[i], "=");
            if (value.length == 1)
                value = toArray(ss[i], ",");
            map.put(value[0], value[1]);
        }/* w  ww.  j  a  va  2s.  c om*/
        return map;
    }

    public static boolean isNull(Object s) {
        if (s == null || s.toString().trim().length() == 0 || s.equals("null"))
            return true;
        else
            return false;
    }

    public static String[] toArray(String s) {
        return toArray(s, ",");
    }

    public static String[] toArray(String s, String delim) {
        if (isNull(s))
            return new String[0];
        return s.split(delim);
    }
}

Related

  1. stringToMap(String value)
  2. stringToMap(String[] strings)
  3. toMap(String componentName, String contextName)
  4. toMap(String jsonStr)
  5. toMap(String parameters)
  6. toMap(String s)
  7. toMap(String source)
  8. toMap(String str)