Java Collection join to String with String separator

Description

Java Collection join to String with String separator

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Main {
  public static void main(String[] argv) throws Exception {
    List list = Arrays.asList("123","abc","demo","demo2s.com",new Date());
    System.out.println(join(list.iterator(), "/"));
  }/* w  w w  .  j a va2s. co  m*/
  /**
   * <p>Joins the elements of the provided <code>Collection</code> into
   * a single String containing the provided elements.</p>
   *
   * <p>No delimiter is added before or after the list. Null objects or empty
   * strings within the iteration are represented by empty strings.</p>
   *
   * <p>See the examples here: {@link #join(Object[],char)}. </p>
   *
   * @param collection  the <code>Collection</code> of values to join together, may be null
   * @param separator  the separator character to use
   * @return the joined String, <code>null</code> if null iterator input
   * @since 2.3
   */
  public static String join(Collection collection, String separator) {
      if (collection == null) {
          return null;
      }
      return join(collection.iterator(), separator);
  }
  /**
   * <p>Joins the elements of the provided <code>Iterator</code> into
   * a single String containing the provided elements.</p>
   *
   * <p>No delimiter is added before or after the list.
   * A <code>null</code> separator is the same as an empty String ("").</p>
   *
   * <p>See the examples here: {@link #join(Object[],String)}. </p>
   *
   * @param iterator  the <code>Iterator</code> of values to join together, may be null
   * @param separator  the separator character to use, null treated as ""
   * @return the joined String, <code>null</code> if null iterator input
   */
  public static String join(Iterator iterator, String separator) {

      // handle null, zero and one elements before building a buffer
      if (iterator == null) {
          return null;
      }
      if (!iterator.hasNext()) {
          return EMPTY;
      }
      Object first = iterator.next();
      if (!iterator.hasNext()) {
          return toString(first);
      }

      // two or more elements
      StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
      if (first != null) {
          buf.append(first);
      }

      while (iterator.hasNext()) {
          if (separator != null) {
              buf.append(separator);
          }
          Object obj = iterator.next();
          if (obj != null) {
              buf.append(obj);
          }
      }
      return buf.toString();
  }
  public static String toString(Object obj) {
    return obj == null ? "" : obj.toString();
}
  public static final String EMPTY = "";
}
/*
 * 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.
 */



PreviousNext

Related