Java Collection Join join(String delimiter, Collection values)

Here you can find the source of join(String delimiter, Collection values)

Description

Joins a provided collection of String -s via provided delimiter.

License

Apache License

Parameter

Parameter Description
delimiter for join
values for joining

Return

joined / null in case that collection is null or empty

Declaration

public static String join(String delimiter, Collection<String> values) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/*from   w  ww.jav a 2s.co m*/
     * Joins a provided collection of {@link String}-s via provided delimiter.
     *
     * @param delimiter
     *            for join
     * @param values
     *            for joining
     * @return joined {@link String} / null in case that collection is null or empty
     */
    public static String join(String delimiter, Collection<String> values) {
        if (values == null || values.isEmpty()) {
            return null;
        }

        Iterator<String> valuesIterator = values.iterator();

        StringBuilder result = new StringBuilder();

        // first value is without delimiter
        result.append(valuesIterator.next());

        // join next values via delimiter
        while (valuesIterator.hasNext()) {
            result.append(delimiter).append(valuesIterator.next());
        }

        return result.toString();
    }
}

Related

  1. join(String delimiter, Collection objects)
  2. join(String delimiter, Collection parts)
  3. join(String delimiter, Collection items)
  4. join(String delimiter, Collection stringCollection)
  5. join(String delimiter, Collection strings)
  6. join(String glue, Collection c)
  7. join(String glue, Collection pieces)
  8. join(String glue, Collection c)
  9. join(String glue, Collection strings)