Java List Join join(List strings, String delimiter)

Here you can find the source of join(List strings, String delimiter)

Description

Join string pieces and separate with a delimiter.

License

Open Source License

Parameter

Parameter Description
strings String pieces to join
delimiter Delimiter to put between string pieces

Return

One merged string

Declaration

public static String join(List<?> strings, String delimiter) 

Method Source Code

//package com.java2s;
/*//from   w  ww.j  a va 2  s . co  m
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership. Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 *
 * This library 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 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Join string pieces and separate with a delimiter. Similar to the perl function of the same name. If strings or delimiter
     * are null, null is returned. Otherwise, at least an empty string will be returned.
     *
     * @param strings String pieces to join
     * @param delimiter Delimiter to put between string pieces
     * @return One merged string
     */
    public static String join(List<?> strings, String delimiter) {
        if (strings == null || delimiter == null) {
            return null;
        }

        StringBuffer str = new StringBuffer();

        // This is the standard problem of not putting a delimiter after the last
        // string piece but still handling the special cases. A typical way is to check every
        // iteration if it is the last one and skip the delimiter - this is avoided by
        // looping up to the last one, then appending just the last one.

        // First we loop through all but the last one (if there are at least 2) and
        // put the piece and a delimiter after it. An iterator is used to walk the list.
        int most = strings.size() - 1;
        if (strings.size() > 1) {
            Iterator<?> iter = strings.iterator();
            for (int i = 0; i < most; i++) {
                str.append(iter.next());
                str.append(delimiter);
            }
        }

        // If there is at least one element, put the last one on with no delimiter after.
        if (strings.size() > 0) {
            str.append(strings.get(most));
        }

        return str.toString();
    }
}

Related

  1. join(List list, String delim)
  2. join(List list, String sep)
  3. join(List objects, String delimiter)
  4. join(List parts)
  5. join(List resultList, String sep)
  6. join(List list1, List list2)
  7. join(List array, char c)
  8. join(List list, String separator)
  9. join(List recycled)