Here you can find the source of mapToStringList(Map, ?> map, String pairDelimiter, String listDelimiter)
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. |
Parameter | Description |
---|---|
IllegalArgumentException | Thrown if any of the keys and/or valuesare null. |
public static String mapToStringList(Map<?, ?> map, String pairDelimiter, String listDelimiter)
//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(); } }