Here you can find the source of join(List
Parameter | Description |
---|---|
list | List of items. |
separator | String separator. |
public static <T> String join(List<T> list, String separator)
//package com.java2s; /******************************************************************************* * Copyright (c) 2011, 2012 Alex Bradley. * 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 * * Contributors:// w w w .jav a 2s. c om * Alex Bradley - initial API and implementation *******************************************************************************/ import java.util.Iterator; import java.util.List; public class Main { /** * Join list elements into a string. * @param list List of items. * @param separator String separator. * @return Items in list concatenated into string with {@code separator} between each item. */ public static <T> String join(List<T> list, String separator) { StringBuffer result = new StringBuffer(); Iterator<T> iter = list.iterator(); while (iter.hasNext()) { result.append(iter.next()); if (iter.hasNext()) { result.append(separator); } } return result.toString(); } }