Here you can find the source of join(Collection> values)
Parameter | Description |
---|---|
values | the values |
public static String join(Collection<?> values)
//package com.java2s; /*// w w w. j a va 2 s . com * Copyright (c) 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ import java.util.Collection; public class Main { /** Default delimiter for the {@link #join} methods. */ public static final String DEFAULT_JOIN_DELIMITER = ", "; /** * Joins a collection of objects' toString() values into a single string, * using the default delimiter of ", " (comma-space). * * @param values the values * @return the delimiter-separated string */ public static String join(Collection<?> values) { if (values == null) return null; return join(values.toArray(new Object[values.size()])); } /** * Joins an array of objects' toString() values into a single string, * using the default delimiter of ", " (comma-space). * * @param values the values * @return the delimiter-separated string */ public static String join(Object[] values) { return join(values, DEFAULT_JOIN_DELIMITER); } /** * Joins a collection of objects' toString() values into a single string, * using the given delimiter. If the collection is null, null is returned. * * @param values the values * @param delim the delimiter * @return the delimiter-separated string */ public static String join(Collection<?> values, String delim) { if (values == null) return null; return join(values.toArray(new Object[values.size()]), delim); } /** * Joins an array of objects' toString() values into a single string, * using the given delimiter. If the array is null, null is returned. * * @param values the values * @param delim the delimiter * @return the delimiter-separated string */ public static String join(Object[] values, String delim) { if (values == null) return null; StringBuilder sb = new StringBuilder(); for (Object v : values) { if (sb.length() > 0) sb.append(delim); sb.append(v); } return sb.toString(); } }