Here you can find the source of join(final List
Parameter | Description |
---|---|
list | list of text items |
public static String join(final List<String> list)
//package com.java2s; //License from project: Open Source License import java.util.List; public class Main { /**/*from ww w.j a va2s . c o m*/ * Joins items of a text list, separating items by comma. * * @param list * list of text items * @return joined list */ public static String join(final List<String> list) { // zero, empty or one element if (list == null) { return null; } else if (list.size() == 0) { return ""; } else if (list.size() == 1) { return list.get(0); } // two or more elements final StringBuilder builder = new StringBuilder(); for (String item : list) { if (builder.length() > 0) { builder.append(", "); } builder.append(item); } return builder.toString(); } /** * Joins items of a text array, separating items by comma. * * @param list * list of text items * @return joined list */ public static String join(final String[] list) { // zero, empty or one element if (list == null) { return null; } else if (list.length == 0) { return ""; } else if (list.length == 1) { return list[0]; } // two or more elements final StringBuilder builder = new StringBuilder(); for (String item : list) { if (builder.length() > 0) { builder.append(", "); } builder.append(item); } return builder.toString(); } }