split string Into Map - Android java.util

Android examples for java.util:Map

Description

split string Into Map

Demo Code

import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main{

    public static Map<String, String> splitIntoMap(String text,
            String delimiter, String separator) {
        HashMap<String, String> ret = new HashMap<String, String>();

        String decodedString = "";
        try {/*from  w  w  w. ja va2  s.c o  m*/
            decodedString = URLDecoder.decode(text, "UTF-8");
        } catch (Exception e) {
        }

        String[] split = decodedString.split(delimiter);
        for (String string : split) {
            if (string.contains(separator)) {
                String[] element = string.split(separator);
                if (element.length == 2)
                    ret.put(element[0], element[1]);
            }
        }
        return ret;
    }

}

Related Tutorials