Java List Join join(String joiner, List joinees)

Here you can find the source of join(String joiner, List joinees)

Description

Joins the strings in joinees , separated by joiner .

License

Apache License

Parameter

Parameter Description
joiner The string to use as the delimiter between each element.
joinees The list of strings to join.

Return

The combined strings in joinees separated by the joiner delimiter.

Declaration

public static String join(String joiner, List<String> joinees) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .  j a va  2 s  .  c  o m*/
 * Copyright 2012 Splunk, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * Joins the strings in {@code joinees}, separated by {@code joiner}.
     *
     * For example, {@code list} contains the strings {@code "a"}, {@code "b"}, 
     * and {@code "c"}. To combine these strings as "a/b/c", use 
     * {@code join("/", list)}.
     *
     * @param joiner The string to use as the delimiter between each element.
     * @param joinees The list of strings to join.
     * @return The combined strings in {@code joinees} separated  by the 
     * {@code joiner} delimiter.
     */
    public static String join(String joiner, List<String> joinees) {
        if (joinees.isEmpty()) {
            return "";
        } else {
            StringBuilder joined = new StringBuilder();
            joined.append(joinees.get(0));
            for (String s : joinees.subList(1, joinees.size())) {
                joined.append(joiner);
                joined.append(s);
            }
            return joined.toString();
        }
    }

    /**
     * Joins the strings in {@code joinees}, separated by {@code joiner}.
     *
     * For example, {@code list} contains the strings {@code "a"}, {@code "b"}, 
     * and {@code "c"}. To combine these strings as "a/b/c", use 
     * {@code join("/", list)}.
     *
     * @param joiner The string to use as the delimiter between each element.
     * @param joinees The array of strings to join.
     * @return The combined strings in {@code joinees} separated  by the 
     * {@code joiner} delimiter.
     */
    public static String join(String joiner, String[] joinees) {
        return join(joiner, Arrays.asList(joinees));
    }
}

Related

  1. join(List... lists)
  2. join(List... ls)
  3. join(List... elements)
  4. join(String _delimiter, List _strings)
  5. join(String delimiter, List strings)
  6. join(String joinString, List strings)
  7. join(String joint, List elements)
  8. join(String sep, List a)
  9. join(String separator, Collection list)