Java List Join join(final List list)

Here you can find the source of join(final List list)

Description

Joins items of a text list, separating items by comma.

License

Open Source License

Parameter

Parameter Description
list list of text items

Return

joined list

Declaration

public static String join(final List<String> list) 

Method Source Code


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

import java.util.List;

public class Main {
    /**/*from ww  w.j  a  va2s .  c o  m*/
     * Joins items of a text list, separating items by comma.
     * 
     * @param list
     *            list of text items
     * @return joined list
     */
    public static String join(final List<String> list) {

        // zero, empty or one element
        if (list == null) {
            return null;
        } else if (list.size() == 0) {
            return "";
        } else if (list.size() == 1) {
            return list.get(0);
        }

        // two or more elements
        final StringBuilder builder = new StringBuilder();
        for (String item : list) {
            if (builder.length() > 0) {
                builder.append(", ");
            }
            builder.append(item);
        }
        return builder.toString();

    }

    /**
     * Joins items of a text array, separating items by comma.
     * 
     * @param list
     *            list of text items
     * @return joined list
     */
    public static String join(final String[] list) {

        // zero, empty or one element
        if (list == null) {
            return null;
        } else if (list.length == 0) {
            return "";
        } else if (list.length == 1) {
            return list[0];
        }

        // two or more elements
        final StringBuilder builder = new StringBuilder();
        for (String item : list) {
            if (builder.length() > 0) {
                builder.append(", ");
            }
            builder.append(item);
        }
        return builder.toString();

    }
}

Related

  1. join(final List list, final String separator)
  2. join(final List things, final String on)
  3. join(final List list, final String delimiter)
  4. join(final List array, final String separator)
  5. join(final List data, String separator)
  6. join(final List list, final String sepa)
  7. join(final List path, final char delimiter)
  8. join(final List strings, final String delimiter)
  9. join(final List strings, final String separator)

  10. HOME | Copyright © www.java2s.com 2016