Here you can find the source of joinMap(String keyValueSeperator, String recordSeperator, Map
Parameter | Description |
---|---|
keyValueSeperator | the string to seperate the key-value pairs |
recordSeperator | the string to use to seperate each key-value pair from other key-value pairs |
map | the map to draw from |
L | the map's key type |
R | the map's value type |
public static <L, R> String joinMap(String keyValueSeperator, String recordSeperator, Map<L, R> map)
//package com.java2s; /*/* w w w . ja v a2s . c om*/ * Copyright (c) 2010 The Broad Institute * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import java.util.*; public class Main { /** * join the key value pairs of a map into one string, i.e. myMap = [A->1,B->2,C->3] with a call of: * joinMap("-","*",myMap) -> returns A-1*B-2*C-3 * * Be forewarned, if you're not using a map that is aware of the ordering (i.e. HashMap instead of LinkedHashMap) * the ordering of the string you get back might not be what you expect! (i.e. C-3*A-1*B-2 vrs A-1*B-2*C-3) * * @param keyValueSeperator the string to seperate the key-value pairs * @param recordSeperator the string to use to seperate each key-value pair from other key-value pairs * @param map the map to draw from * @param <L> the map's key type * @param <R> the map's value type * @return a string representing the joined map */ public static <L, R> String joinMap(String keyValueSeperator, String recordSeperator, Map<L, R> map) { if (map.size() < 1) { return null; } String joinedKeyValues[] = new String[map.size()]; int index = 0; for (L key : map.keySet()) { joinedKeyValues[index++] = String.format("%s%s%s", key.toString(), keyValueSeperator, map.get(key).toString()); } return join(recordSeperator, joinedKeyValues); } /** * join an array of strings given a seperator * @param separator the string to insert between each array element * @param strings the array of strings * @return a string, which is the joining of all array values with the separator */ public static String join(String separator, String[] strings) { return join(separator, strings, 0, strings.length); } public static String join(String separator, String[] strings, int start, int end) { if ((end - start) == 0) { return ""; } StringBuilder ret = new StringBuilder(strings[start]); for (int i = start + 1; i < end; ++i) { ret.append(separator); ret.append(strings[i]); } return ret.toString(); } /** * Returns a string of the form elt1.toString() [sep elt2.toString() ... sep elt.toString()] for a collection of * elti objects (note there's no actual space between sep and the elti elements). Returns * "" if collection is empty. If collection contains just elt, then returns elt.toString() * * @param separator the string to use to separate objects * @param objects a collection of objects. the element order is defined by the iterator over objects * @param <T> the type of the objects * @return a non-null string */ public static <T> String join(final String separator, final Collection<T> objects) { if (objects.isEmpty()) { // fast path for empty collection return ""; } else { final Iterator<T> iter = objects.iterator(); final T first = iter.next(); if (!iter.hasNext()) // fast path for singleton collections return first.toString(); else { // full path for 2+ collection that actually need a join final StringBuilder ret = new StringBuilder(first.toString()); while (iter.hasNext()) { ret.append(separator); ret.append(iter.next().toString()); } return ret.toString(); } } } }