Java Collection Join join(Collection lst, String delim)

Here you can find the source of join(Collection lst, String delim)

Description

joins a list into a string.

License

Open Source License

Parameter

Parameter Description
lst a parameter
delim a parameter

Declaration

public static String join(Collection lst, String delim) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//  ww  w  .  ja  v  a2  s . c o m
     * joins a list into a string.  similar to methods available IN ANY OTHER LANGUAGE
     * @param lst
     * @param delim
     * @return
     */
    public static String join(Collection lst, String delim) {
        return joinCol(lst, delim);
    }

    public static String join(List lst, String delim) {
        //this method should not be necessary, but exceptions thrown in test env show that it is..
        //strange..
        return joinCol((Collection) lst, delim);
    }

    /**
     * split into separate method so we don't get any accidental recursion
     * between join(list) and join(collection).
     * @param lst
     * @param delim
     * @return
     */
    private static String joinCol(Collection lst, String delim) {
        StringBuilder builder = new StringBuilder();
        for (Object obj : lst) {
            if (obj != null)
                builder.append(obj.toString());
            builder.append(delim);
        }

        builder.delete(builder.length() - delim.length(), builder.length());
        return builder.toString();
    }
}

Related

  1. join(Collection collection, String separator, boolean trailing)
  2. join(Collection data)
  3. join(Collection elements, String separator)
  4. join(Collection items)
  5. join(Collection list, char separator)
  6. join(Collection objects, String glue)
  7. join(Collection paramCollection, String paramString)
  8. join(Collection parts, String sep)
  9. join(Collection s, String delimiter)