Java List Join join(Collection stringList, String seperator)

Here you can find the source of join(Collection stringList, String seperator)

Description

join

License

Open Source License

Declaration

public static String join(Collection<?> stringList, String seperator) 

Method Source Code


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

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static String join(Collection<?> stringList, String seperator) {
        return join(stringList, seperator, "%s");
    }/*  www .j  a va2  s  . co m*/

    public static String join(Collection<?> stringList, String seperator, String itemFormat) {
        StringBuffer buffer = new StringBuffer();

        Iterator<?> it = stringList.iterator();

        for (int i = 0; it.hasNext(); i++) {
            if (i > 0) {
                buffer.append(seperator);
            }

            buffer.append(printf(itemFormat, it.next().toString()));
        }

        return buffer.toString();
    }

    public static String printf(String format, Object... args) {
        return String.format(format, args);
    }

    public static String toString(Collection<?> itemList) {
        return printf("[%s]", join(itemList, ", ", "\"%s\""));
    }

    public static String toString(Map<?, ?> itemMap) {
        ArrayList<String> entryStrings = new ArrayList<String>();

        for (Map.Entry<?, ?> entry : itemMap.entrySet()) {
            String entryString = printf("%s: \"%s\"", entry.getKey(), entry.getValue());
            entryStrings.add(entryString);
        }

        return printf("{%s}", join(entryStrings, ", "));
    }
}

Related

  1. getJoinKeyValues(List tuple, List joinKeyIndices)
  2. join(char separator, List seqs)
  3. join(CharSequence delimiter, Iterable list)
  4. join(CharSequence delimiter, List elements)
  5. join(Collection list, String separator)
  6. join(Collection list, String delimiter)
  7. join(Collection list, String seperator)
  8. join(final List list, final String conjunction)
  9. join(final List toJoin)