Java Collection Join join(Collection list, String separator)

Here you can find the source of join(Collection list, String separator)

Description

join

License

Open Source License

Declaration

public static String join(Collection<? extends Object> list, String separator) 

Method Source Code


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

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static String join(Collection<? extends Object> list, String separator) {
        StringBuilder sb = new StringBuilder();

        for (Object o : list) {
            sb.append(o.toString());//from  w w  w. j ava  2s . co m
            sb.append(separator);
        }

        if (sb.length() > 0 && separator.length() > 0) {
            sb.delete(sb.length() - separator.length(), sb.length());
        }

        return sb.toString();
    }

    public static String join(Map<? extends Object, ? extends Object> map, String keyValueSeparator,
            String entrySeparator) {
        StringBuilder sb = new StringBuilder();

        for (Entry<? extends Object, ? extends Object> entry : map.entrySet()) {
            sb.append(entry.getKey().toString());
            sb.append(keyValueSeparator);
            sb.append(entry.getValue().toString());
            sb.append(entrySeparator);
        }

        if (sb.length() > 0 && entrySeparator.length() > 0) {
            sb.delete(sb.length() - entrySeparator.length(), sb.length());
        }

        return sb.toString();
    }
}

Related

  1. join(Collection var0, Object var1)
  2. join(Collection c, String joinWith)
  3. join(Collection collection, String joinString)
  4. join(Collection collection, String separator)
  5. join(Collection elements, String separator)
  6. join(Collection objects, String delim)
  7. join(Collection strings, String separator)
  8. join(Collection strs, String separator)
  9. join(Collection c, String delim)