Java Collection Concatenate concatenate(Collection list)

Here you can find the source of concatenate(Collection list)

Description

concatenate

License

Open Source License

Declaration

public static String concatenate(Collection<?> list) 

Method Source Code

//package com.java2s;
/*/*from  w ww.jav a  2 s .  co  m*/
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

import java.util.Collection;

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

    public static String concatenate(Collection<?> list) {
        return concatenate(list, DEFAULT_DELIMITER);
    }

    public static String concatenate(Collection<?> list, String delimiter) {
        if (list == null || list.isEmpty()) {
            return EMPTY;
        }
        if (delimiter == null) {
            delimiter = EMPTY;
        }
        StringBuilder sb = new StringBuilder();

        for (Object object : list) {
            sb.append(object.toString());
            sb.append(delimiter);
        }

        sb.setLength(sb.length() - delimiter.length());
        return sb.toString();
    }
}

Related

  1. concat(String[] first, Collection second)
  2. concatCollection(Collection collection)
  3. concatElements(final Collection c, final String separator)
  4. concatenate(Collection collections)
  5. concatenate(Collection coll)
  6. concatenate(final Collection objects, final String separator)
  7. concatenatedLTL(Collection ltl)
  8. concatenateIds(final Collection ids)
  9. concatenateWithComma(Collection collection)