Here you can find the source of join(List
Parameter | Description |
---|---|
list | List to create a string from. |
delimiter | Delimiter to use between elements. |
T | Type of elements in the list. |
public static <T> String join(List<T> list, String delimiter)
//package com.java2s; /****************************************************************************** * Copyright (C) 2014 Yevgeny Krasik * * * * 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.List; public class Main { /**/* www. j a va 2s .c o m*/ * Create a string out of the list's elements using the given delimiter. * * @param list List to create a string from. * @param delimiter Delimiter to use between elements. * @param <T> Type of elements in the list. * @return A string created from the given list's elements, delimited by the given delimiter. */ public static <T> String join(List<T> list, String delimiter) { if (list.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); for (T element : list) { sb.append(element); sb.append(delimiter); } sb.delete(sb.length() - delimiter.length(), sb.length()); return sb.toString(); } }