Here you can find the source of joinValues(Map
Parameter | Description |
---|---|
map | a map containing collections as its values |
separator | the separator to be used between each value |
K | the type of the key used in the given map |
V | the type of the collection of values associated with each key of the map |
public static final <K, V extends Iterable> Map<K, String> joinValues(Map<K, V> map, String separator)
//package com.java2s; /*// w ww .j ava 2s.c om * Copyright (c) 2013 Univocity Software Pty Ltd. All rights reserved. * This file is subject to the terms and conditions defined in file * 'LICENSE.txt', which is part of this source code package. */ import java.util.*; public class Main { /** * Joins each collection of values in a given {@code Map} into their {@code String} * representation, with a given separator between each value. * * @param map a map containing collections as its values * @param separator the separator to be used between each value * @param <K> the type of the key used in the given map * @param <V> the type of the collection of values associated with each key of the map * @return the resulting map where each key of the given input map is associated * with the String representation of all non-null values in the collection * associated with the key. */ public static final <K, V extends Iterable> Map<K, String> joinValues(Map<K, V> map, String separator) { if (map == null || map.isEmpty()) { return Collections.emptyMap(); } LinkedHashMap<K, String> out = new LinkedHashMap<K, String>(); for (Map.Entry<K, V> e : map.entrySet()) { out.put(e.getKey(), join(e.getValue(), separator)); } return out; } /** * Joins the {@code String} representation of all non-null values in a given * collection into a {@code String}, with a given separator between each value. * * @param values the values to be joined. Nulls are skipped. * @param separator the separator to use between each value * @return a String with all non-null values in the given collection. */ public static final String join(Iterable<?> values, String separator) { if (values == null) { return ""; } StringBuilder out = new StringBuilder(64); for (Object value : values) { if (value != null) { if (out.length() != 0) { out.append(separator); } out.append(value); } } return out.toString(); } }