Java Collection to String collectionToCommaSeparatedString(Collection elementCollection)

Here you can find the source of collectionToCommaSeparatedString(Collection elementCollection)

Description

Collection to comma separated string string.

License

Open Source License

Parameter

Parameter Description
elementCollection the element collection

Return

the string

Declaration

public static String collectionToCommaSeparatedString(Collection<String> elementCollection) 

Method Source Code

//package com.java2s;
/*-/*www .  ja v  a  2 s  . co  m*/
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**
     * Collection to comma separated string string.
     *
     * @param elementCollection the element collection
     * @return the string
     */
    public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
        List<String> list = new ArrayList<>();
        elementCollection.stream().forEach(element -> list.add(element));
        return listToSeparatedString(list, ',');
    }

    /**
     * Converts array of strings to string separated with specified character.
     *
     * @param list      array of strings
     * @param separator the separator
     * @return the string
     */
    public static String listToSeparatedString(List<String> list, char separator) {
        String res = null;
        if (null != list) {
            StringBuilder sb = new StringBuilder();
            int sz = list.size();
            for (int i = 0; i < sz; i++) {
                if (i > 0) {
                    sb.append(separator);
                }
                sb.append(list.get(i));
            }
            res = sb.toString();
        }
        return res;
    }
}

Related

  1. buildString(Collection args, String seperator, int startingArg, int maxLength)
  2. buildString(Collection keys)
  3. buildStringFromListForNuma(Collection list)
  4. collectionToCommaDelimitedString(Collection items)
  5. collectionToCommaDelimitedString(Collection list)
  6. collectionToCommaSeparatedString(List list)
  7. collectionToCommaString(Collection bundleSelection)
  8. collectionToCSString(Collection col)
  9. collectionToDelimitedString( Iterable iterable)