Java Map to String mapToString(Map map)

Here you can find the source of mapToString(Map map)

Description

Outputs entries from a Map to a String .

License

Open Source License

Parameter

Parameter Description
map Input map

Return

String representing the map

Declaration

public static String mapToString(Map map) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:// ww  w .  j  a  v a  2  s  .c  om
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.Iterator;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * Outputs entries from a {@code Map} to a {@code String}. Default 
     * separator between each key and value is '=', and separator between key-value pair
     * is ', '.
     *
     * @param map Input map
     * @return {@code String} representing the map
     * 
     */
    public static String mapToString(Map map) {
        return mapToString(map, "=", ", ");
    }

    /**
     * Outputs entries from a {@code Map} to a {@code String}.
     *
     * @param map Input map
     * @param keyValueSeparator Separator between keys and values
     * @param entrySeparator Separator between key-value pairs
     * @return {@code String} representing the map
     * 
     */
    public static String mapToString(Map map, String keyValueSeparator, String entrySeparator) {
        StringBuilder string = new StringBuilder();

        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            if (string.length() > 0)
                string.append(entrySeparator);

            string.append(entry.getKey());
            string.append(keyValueSeparator);
            string.append(entry.getValue());
        }

        return string.toString();
    }
}

Related

  1. mapToStr(Map map)
  2. mapToString(final Map m)
  3. mapToString(final Map m)
  4. mapToString(final Map map)
  5. mapToString(Map map)
  6. mapToString(Map map)
  7. mapToString(Map map)
  8. mapToString(Map map)
  9. mapToString(Map map)