Java Iterable Item print(Iterable coll, String sep)

Here you can find the source of print(Iterable coll, String sep)

Description

Prints all items in a collection to a string, separated by a custom separator.

License

Open Source License

Parameter

Parameter Description
coll Set of items to be printed
sep The separator to add between each elements.
T a parameter

Return

A string representation of the collection.

Declaration

public static <T> String print(Iterable<T> coll, String sep) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*from ww  w  .  j av a  2  s.co m*/
     * Prints all items in a collection to a string, separated by a custom separator.
     * @param coll Set of items to be printed
     * @param sep The separator to add between each elements.
     * @param <T>
     * @return A string representation of the collection.
     */
    public static <T> String print(Iterable<T> coll, String sep) {
        StringBuilder builder = new StringBuilder();
        Iterator<T> it = coll.iterator();
        while (it.hasNext()) {
            T item = it.next();
            builder.append(item.toString());
            if (it.hasNext())
                builder.append(sep);
        }
        return builder.toString();
    }

    /**
     * Prints all items in an array to a string, separated by a custom separator.
     * @param coll Set of items to be printed
     * @param sep The separator to add between each elements.
     * @param <T>
     * @return A string representation of the collection.
     */
    public static <T> String print(T[] coll, String sep) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < coll.length; i++) {
            builder.append(coll[i]);
            if (i < coll.length - 1)
                builder.append(sep);
        }
        return builder.toString();
    }
}

Related

  1. isIterable(Object obj)
  2. isPrefix(Iterable suspectedPrefix, Iterable container)
  3. iterableContains(Iterable i, E match)
  4. maxes(Iterable elements)
  5. prefix(final Iterable lines, final String prefix)
  6. reverse(Iterable values)
  7. single(Iterable source)
  8. toArray(Iterable elements)