Here you can find the source of join(Collection extends Object> collection, String separator)
Parameter | Description |
---|---|
collection | a parameter |
separator | a parameter |
public static String join(Collection<? extends Object> collection, String separator)
//package com.java2s; /**/*from ww w . j ava 2 s . c om*/ * Copyright (c) 2013 M. Alexander Nugent Consulting <i@alexnugent.name> * * M. Alexander Nugent Consulting Research License Agreement * Non-Commercial Academic Use Only * * This Software is proprietary. By installing, copying, or otherwise using this * Software, you agree to be bound by the terms of this license. If you do not agree, * do not install, copy, or use the Software. The Software is protected by copyright * and other intellectual property laws. * * You may use the Software for non-commercial academic purpose, subject to the following * restrictions. You may copy and use the Software for peer-review and methods verification * only. You may not create derivative works of the Software. You may not use or distribute * the Software or any derivative works in any form for commercial or non-commercial purposes. * * Violators will be prosecuted to the full extent of the law. * * All rights reserved. No warranty, explicit or implicit, provided. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARR?ANTY 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.Collection; import java.util.Iterator; public class Main { /** * Joins a collection of Objects separated by a specified separator * * @param collection * @param separator * @return the joined String */ public static String join(Collection<? extends Object> collection, String separator) { if (collection == null) { return null; } Iterator iterator = collection.iterator(); // handle null, zero and one elements before building a buffer if (iterator == null) { return null; } if (!iterator.hasNext()) { return ""; } Object first = iterator.next(); if (!iterator.hasNext()) { return first == null ? "" : first.toString(); } // two or more elements StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small if (first != null) { buf.append(first); } while (iterator.hasNext()) { if (separator != null) { buf.append(separator); } Object obj = iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); } }