Java Map to String mapToString(final Map m)

Here you can find the source of mapToString(final Map m)

Description

Transforms a map to a string containing name-value pairs separated by '&'.

License

Open Source License

Parameter

Parameter Description
m map to transform

Return

resulting string

Declaration

public static String mapToString(final Map<String, String> m) 

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  .com*/
 */

import java.util.Map;

public class Main {
    /**
     * Transforms a map to a string containing name-value pairs separated by '&'.
     * @param m map to transform
     * @return resulting string
     */
    public static String mapToString(final Map<String, String> m) {
        final StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> e : m.entrySet()) {
            sb.append(e.getKey().replace("&", "&&")).append('=');
            final String value = e.getValue();
            if (value != null)
                sb.append(e.getValue().replace("&", "&&"));
            sb.append('&');
        }
        sb.setLength(sb.length() - 1);
        return sb.toString();
    }
}

Related

  1. mapToStr(java.util.Map hm, char sep1, char sep2)
  2. mapToStr(Map map)
  3. mapToStr(Map map)
  4. mapToString(final Map m)
  5. mapToString(final Map map)
  6. mapToString(Map map)
  7. mapToString(Map map)
  8. mapToString(Map map)