Here you can find the source of join(Collection> collection, String join)
Parameter | Description |
---|---|
collection | The collection (List, Set) to be joined. |
join | The value to be joined between each part. |
public static String join(Collection<?> collection, String join)
//package com.java2s; /*/* ww w .ja va 2 s. c om*/ * net/balusc/util/StringUtil.java * * Copyright (C) 2007 BalusC * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU Lesser General Public License as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library. * If not, see <http://www.gnu.org/licenses/>. */ import java.util.Collection; import java.util.Iterator; public class Main { /** * Join the given collection with the given join value. * @param collection The collection (List, Set) to be joined. * @param join The value to be joined between each part. * @return The joined collection. */ public static String join(Collection<?> collection, String join) { StringBuilder builder = new StringBuilder(); for (Iterator<?> iter = collection.iterator(); iter.hasNext();) { builder.append(iter.next()); if (iter.hasNext()) { builder.append(join); } } return builder.toString(); } /** * Join the given ordinary array with the given join value. * @param objects The ordinary array (String[], Integer[], etc) to be joined. * @param join The value to be joined between each part. * @return The joined array. */ public static String join(Object[] objects, String join) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < objects.length;) { builder.append(objects[i]); if (++i < objects.length) { builder.append(join); } } return builder.toString(); } }