Java String to Map stringToMap(final String s)

Here you can find the source of stringToMap(final String s)

Description

Converts a string containing name-value pairs to a map.

License

Open Source License

Parameter

Parameter Description
s string to transform

Return

null if input is null

Declaration

public static Map<String, String> stringToMap(final String s) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2011-2014 Pushing Inertia
 * All rights reserved.  http://pushinginertia.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./* w w  w .j a  v a2 s .  co  m*/
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Converts a string containing name-value pairs to a map. Values that are undefined (i.e., string length is zero)
     * are added as null rather than not empty strings.
     * @param s string to transform
     * @return null if input is null
     */
    public static Map<String, String> stringToMap(final String s) {
        if (s == null)
            return null;
        final Map<String, String> m = new HashMap<String, String>();

        final StringBuilder sb = new StringBuilder();
        int equalSignIdx = -1;
        int ampersandCount = 0;
        for (char c : s.toCharArray()) {
            // check if we're at the end of one or more ampersands
            if (c != '&' && ampersandCount > 0) {
                // first append the number of ampersands encountered, divided by 2
                for (int i = 0; i < ampersandCount / 2; i++)
                    sb.append('&');

                if (ampersandCount % 2 > 0) {
                    // hit the end of a name-value pair
                    if (equalSignIdx > 0) {
                        final String value = sb.substring(equalSignIdx);
                        m.put(sb.substring(0, equalSignIdx), value.length() == 0 ? null : value);
                    }
                    equalSignIdx = -1;
                    sb.setLength(0);
                }

                ampersandCount = 0;
            }
            switch (c) {
            case '&':
                ampersandCount++;
                break;
            case '=':
                if (equalSignIdx < 0)
                    equalSignIdx = sb.length();
                break;
            default:
                sb.append(c);
                break;
            }
        }
        for (int i = 0; i < ampersandCount / 2; i++)
            sb.append('&');
        if (equalSignIdx > 0) {
            final String value = sb.substring(equalSignIdx);
            m.put(sb.substring(0, equalSignIdx), value.length() == 0 ? null : value);
        }

        return m;
    }
}

Related

  1. stringToCharMap(String s)
  2. stringToMap(final String s)
  3. stringToMap(String append)
  4. stringToMap(String source, String delimiter)
  5. stringToMap(String src)