Here you can find the source of join(String delimiter, Collection> items)
Parameter | Description |
---|---|
delimiter | separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter. |
items | list with items to be joined |
public static String join(String delimiter, Collection<?> items)
//package com.java2s; /*//from w w w.j av a 2 s. com * The MIT License (MIT) * <p/> * Copyright (c) 2015 Maksym Dominichenko * <p/> * 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: * <p/> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * <p/> * 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 { /** * Joins string items into one string separated by delimiter. * * @param delimiter * separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter. * @param items * array with items to be joined * @return all items joined into one string and separated by delimiter. */ public static String join(String delimiter, String... items) { if (isEmpty(items)) return ""; StringBuilder result = new StringBuilder(); boolean delimOk = !isEmpty(delimiter); for (Object item : items) { if (delimOk && result.length() > 0) result.append(delimiter); result.append(item); } return result.toString(); } /** * Joins string items into one string separated by delimiter. * * @param delimiter * separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter. * @param items * list with items to be joined * @return all items joined into one string and separated by delimiter. */ public static String join(String delimiter, Collection<?> items) { if (isEmpty(items)) return ""; StringBuilder result = new StringBuilder(); boolean delimOk = !isEmpty(delimiter); for (Object item : items) { if (delimOk && result.length() > 0) result.append(delimiter); result.append(item); } return result.toString(); } /** * Helper to check if the String is {@code null} or empty.<br/> * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param string * A string to be checked * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise. */ public static boolean isEmpty(String string) { return string == null || string.length() == 0; } /** * Helper to check if the array is {@code null} or empty.<br/> * * @param array * An array to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Object[] array) { return array == null || array.length == 0; } /** * Helper to check if the collection is {@code null} or empty.<br/> * {@link Collection#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param collection * A collection to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } /** * Helper to check if the map is {@code null} or empty.<br/> * {@link Map#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param map * A map to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Map<?, ?> map) { return map == null || map.isEmpty(); } }