Here you can find the source of join(final Collection
Parameter | Description |
---|---|
T | a parameter |
objs | a parameter |
delimiter | a parameter |
public static <T> String join(final Collection<T> objs, String delimiter)
//package com.java2s; /*/* w w w . jav a2 s. co m*/ * CDDL HEADER START * * The contents of this file are subject to the terms of the Common Development and Distribution * License, Version 1.0 only (the "License"). You may not use this file except in compliance with * the License. * * You can obtain a copy of the license at license/ESCIDOC.LICENSE or * http://www.escidoc.org/license. See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each file and include the License * file at license/ESCIDOC.LICENSE. If applicable, add the following below this CDDL HEADER, with * the fields enclosed by brackets "[]" replaced with your own identifying information: Portions * Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ import java.util.Collection; import java.util.Iterator; public class Main { /** * Join elements of any collection with delimiter * * @param <T> * @param objs * @param delimiter * @return a joined string */ public static <T> String join(final Collection<T> objs, String delimiter) { if (objs == null || objs.isEmpty()) return ""; if (!checkVal(delimiter)) delimiter = ""; Iterator<T> iter = objs.iterator(); StringBuffer buffer = new StringBuffer(); while (iter.hasNext()) { Object o = iter.next(); String str = ""; if (o != null) { str = o.toString(); // empty strings will be omitted! if (checkVal(str)) buffer.append(str).append(delimiter); } } String result = buffer.toString(); result = result.substring(0, result.length() - delimiter.length()); return result; } /** * Returns true if val is not null && not empty String * * @param val * @return first not null && not empty String */ public static boolean checkVal(String val) { return (val != null && !val.trim().equals("")); } }