Java Utililty Methods Collection Join

List of utility methods to do Collection Join

Description

The list of methods to do Collection Join are organized into topic(s).

Method

StringjoinSorted(Collection s, String delimiter)
join Sorted
List list = new ArrayList(s);
Collections.sort(list);
return join(list, delimiter);
StringjoinString(Collection values, String delimiter)
join String
StringBuilder sb = new StringBuilder();
for (String value : values) {
    sb.append(value);
    sb.append(delimiter);
if (sb.length() >= delimiter.length()) {
    sb.setLength(sb.length() - delimiter.length());
return sb.toString();
StringjoinStringCollection(Collection collection, String separator)
join String Collection
if (collection == null)
    return null;
if (collection.size() == 0)
    return "";
StringBuilder b = new StringBuilder();
Iterator<String> i = collection.iterator();
while (i.hasNext()) {
    if (b.length() > 0)
...
StringjoinStrings(Collection strings)
Join a collection of strings into a single String object, in the order indicated by the collection's iterator.
StringBuffer buf = new StringBuffer();
if (strings == null) {
    throw new NullPointerException("Received null collection");
for (Iterator it = strings.iterator(); it.hasNext();) {
    Object obj = it.next();
    if (obj == null) {
        throw new NullPointerException("Received collection containing null object");
...
StringjoinStrings(Collection names)
join Strings
StringBuilder sb = new StringBuilder(1000);
for (String name : names) {
    if (sb.length() > 0) {
        sb.append(", ");
    sb.append(name);
return sb.toString();
...
StringjoinStrings(Collection strings)
Join a collection of strings into a single String object, in the order indicated by the collection's iterator.
StringBuilder buf = new StringBuilder();
if (strings == null) {
    throw new NullPointerException("Received null collection");
for (String s : strings) {
    buf.append(s);
return buf.toString();
...
StringjoinStrings(String delim, Collection strs)
Joins a collection of strings together sepeated by a delim in the middle
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String str : strs) {
    if (first) {
        first = false;
    } else {
        sb.append(delim);
    sb.append(str);
return sb.toString();
CollectionjoinStringValues(Collection values)
Join the String values while preserving the non-String values.
StringBuilder buffer = new StringBuilder();
List<Object> betterValues = new ArrayList<>();
for (Object value : values) {
    if (value instanceof String) {
        if (buffer.length() > 0) {
            buffer.append(" ");
        buffer.append((String) value);
...
voidjoinTo(Collection data, String sep, StringBuilder sb)
join To
if (data == null || data.isEmpty())
    return;
Iterator<?> iterator = data.iterator();
sb.append(String.valueOf(iterator.next()));
while (iterator.hasNext()) {
    Object obj = iterator.next();
    sb.append(sep).append(String.valueOf(obj));
StringjoinTogether(Collection items, String delim)
join Together
StringBuffer ret = new StringBuffer();
for (Iterator<String> it = items.iterator(); it.hasNext();) {
    ret.append(it.next());
    if (it.hasNext()) {
        ret.append(delim);
return ret.toString();
...