Java Map to String mapToStringList(Map map, String pairDelimiter, String listDelimiter)

Here you can find the source of mapToStringList(Map map, String pairDelimiter, String listDelimiter)

Description

Takes a map of objects and converts their keys and values to a list where each key and value are converted to a string by their toString() methods, the pairs are joined with a 'pairDelimiter' between them and the pairs are joined with other pairs with a 'listDelimiter' between them.

The keys and value cannot be null or an exception will be thrown.

License

Apache License

Parameter

Parameter Description
map The map of items.
pairDelimiter The string to be placed between the key and value while generating the list.
listDelimiter The string to be placed between the already joined pairs while generating the list.

Exception

Parameter Description
IllegalArgumentException Thrown if any of the keys and/or valuesare null.

Return

A String representing all of the pairs and list items together.

Declaration

public static String mapToStringList(Map<?, ?> map, String pairDelimiter, String listDelimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 The Regents of the University of California
 * /*from  www.ja  v  a  2  s  . c o  m*/
 * 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.
 ******************************************************************************/

import java.util.Map;

public class Main {
    /**
     * Takes a map of objects and converts their keys and values to a list 
     * where each key and value are converted to a string by their toString()
     * methods, the pairs are joined with a 'pairDelimiter' between them and
     * the pairs are joined with other pairs with a 'listDelimiter' between
     * them.<br />
     * <br />
     * The keys and value cannot be null or an exception will be thrown.
     * 
     * @param map The map of items.
     * 
     * @param pairDelimiter The string to be placed between the key and value
     *                   while generating the list.
     * 
     * @param listDelimiter The string to be placed between the already joined
     *                   pairs while generating the list.
     * 
     * @return A String representing all of the pairs and list items together.
     * 
     * @throws IllegalArgumentException Thrown if any of the keys and/or values
     *                            are null.
     */
    public static String mapToStringList(Map<?, ?> map, String pairDelimiter, String listDelimiter) {
        boolean firstPass = true;
        StringBuilder resultBuilder = new StringBuilder();

        for (Object key : map.keySet()) {
            Object value = map.get(key);

            if (value == null) {
                throw new IllegalArgumentException("The map cannot contain null values.");
            }

            if (firstPass) {
                firstPass = false;
            } else {
                resultBuilder.append(listDelimiter);
            }

            resultBuilder.append(key.toString()).append(pairDelimiter).append(value.toString());
        }

        return resultBuilder.toString();
    }
}

Related

  1. mapToString(Map map, String splitter)
  2. mapToString(String aName, Map aMap, String aPrefix, String aSuffix, String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix, String anArraySeparator, String anEscapeChars, char anEscapeSymbol)
  3. mapToString(String itemName, Map map)
  4. mapToStringArray(Map map)
  5. mapToStringBuilder(Map m)
  6. mapToStringMap(final Object map)
  7. toQueryString(Map ps)
  8. toStr(Map msg)
  9. toString(final Map tokens)