Here you can find the source of collectionToDelimitedString(Collection> coll, String delim, String prefix, String suffix)
Parameter | Description |
---|---|
coll | the Collection to display |
delim | the delimiter to use (probably a ",") |
prefix | the String to start each element with |
suffix | the String to end each element with |
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)
//package com.java2s; /*//from w w w. j ava 2 s.c o m * Copyright 2002-2012 the original author or authors. * * 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.*; public class Main { /** * Convenience method to return a Collection as a delimited (e.g. CSV) * String. E.g. useful for {@code toString()} implementations. * * @param coll the Collection to display * @param delim the delimiter to use (probably a ",") * @param prefix the String to start each element with * @param suffix the String to end each element with * @return the delimited String */ public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) { if (coll == null || coll.isEmpty()) { return ""; } StringBuilder sb = new StringBuilder(); Iterator<?> it = coll.iterator(); while (it.hasNext()) { sb.append(prefix).append(it.next()).append(suffix); if (it.hasNext()) { sb.append(delim); } } return sb.toString(); } /** * Convenience method to return a Collection as a delimited (e.g. CSV) * String. E.g. useful for {@code toString()} implementations. * * @param coll the Collection to display * @param delim the delimiter to use (probably a ",") * @return the delimited String */ public static String collectionToDelimitedString(Collection<?> coll, String delim) { return collectionToDelimitedString(coll, delim, "", ""); } /** * Check whether the given String is empty. * <p>This method accepts any Object as an argument, comparing it to * {@code null} and the empty String. As a consequence, this method * will never return {@code true} for a non-null non-String object. * <p>The Object signature is useful for general attribute handling code * that commonly deals with Strings but generally has to iterate over * Objects since attributes may e.g. be primitive value objects as well. * * @param str the candidate String * @since 3.2.1 */ public static boolean isEmpty(Object str) { return (str == null || "".equals(str)); } public static boolean equals(String str1, String str2) { return str1 == null ? str2 == null : str1.equals(str2); } }