Java Collection Join join(String delimiter, Collection objects)

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

Description

join

License

Open Source License

Declaration

public static String join(String delimiter, Collection<?> objects) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static String join(String delimiter, Object... objects) {
        return join(delimiter, Arrays.asList(objects));
    }//from  w ww .j a va 2 s .  c  o m

    public static String join(String delimiter, Collection<?> objects) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iterator = filterNulls(objects).iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            sb.append(String.valueOf(next));
            if (iterator.hasNext()) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    public static List<?> filterNulls(Collection<?> objects) {
        List<Object> notNulls = new ArrayList<>();
        for (Object o : objects) {
            if (o != null) {
                notNulls.add(o);
            }
        }
        return notNulls;
    }
}

Related

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