Java Collection Join join(Collection data)

Here you can find the source of join(Collection data)

Description

Join the given Collection using the DEFAULT_DELIMITER.

License

Open Source License

Parameter

Parameter Description
data The collection

Return

The resulting String

Declaration


public static String join(Collection data) 

Method Source Code


//package com.java2s;

import java.util.Iterator;

import java.util.Collection;

public class Main {
    public static final String DEFAULT_DELIMITER = ",";

    /** Join the given Object array using the DEFAULT_DELIMITER.
        //from  w ww  .  ja  va2s.  c o m
    @param data The data array
    @return The resulting String
    */

    public static String join(Object[] data) {
        return join(data, DEFAULT_DELIMITER);
    }

    /** Join the given Object array using the given delimiter.  The toString()
    method on each Object will be invoked to retrieve a String representation
    of the Object.
        
    @param data The data array
    @param delimiter The delimiter String
    @return The resulting String
    */

    public static String join(Object[] data, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            buffer.append(data.toString());
            if (i < data.length - 1) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    /** Join the given Collection using the DEFAULT_DELIMITER.
        
    @param data The collection
    @return The resulting String
    */

    public static String join(Collection data) {
        return join(data, DEFAULT_DELIMITER);
    }

    /** Join the given Collection using the given delimiter.  The toString()
    method on each Object will be invoked to retrieve a String representation
    of the Object.
        
    @param data The collection
    @param delimiter The delimiter String
    @return The resulting String
    */

    public static String join(Collection data, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = data.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next().toString());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
}

Related

  1. join(Collection collection, String separator)
  2. join(Collection collection, String separator)
  3. join(Collection collection, String separator)
  4. join(Collection collection, String separator)
  5. join(Collection collection, String separator, boolean trailing)
  6. join(Collection elements, String separator)
  7. join(Collection items)
  8. join(Collection list, char separator)
  9. join(Collection lst, String delim)