Java List Join join(List list, String separator)

Here you can find the source of join(List list, String separator)

Description

Join list elements into a string.

License

Open Source License

Parameter

Parameter Description
list List of items.
separator String separator.

Return

Items in list concatenated into string with separator between each item.

Declaration

public static <T> String join(List<T> list, String separator) 

Method Source Code

//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();
    }
}

Related

  1. join(List entries, String separator)
  2. join(List list, String delim)
  3. join(List list, String delim)
  4. join(List list, String delimiter)
  5. join(List list, String joiner)
  6. join(List objects, String separator)
  7. join(List objs, String delim)
  8. join(List target, String splite)
  9. join(List tokens, String delimiter)