Java String Join joinValues(Map map, String separator)

Here you can find the source of joinValues(Map map, String separator)

Description

Joins each collection of values in a given Map into their String representation, with a given separator between each value.

License

Open Source License

Parameter

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

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.

Declaration

public static final <K, V extends Iterable> Map<K, String> joinValues(Map<K, V> map, String separator) 

Method Source Code


//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();
    }
}

Related

  1. joinPath(String part1, String part2)
  2. joinPaths(String... paths)
  3. joinRepeat(int size, String s, String delim)
  4. joinSplit(String record, String regex)
  5. joinString(String separator, String... elements)
  6. joinWithSeparation(String a, String separator, String b)
  7. merge(ArrayList pArrayList, String pJoin)
  8. mergeDependencies(Set bundleDeps, List deps, Map disjointSets)
  9. prefixed_join(String padder, Vector v, boolean quoted)