Here you can find the source of collectionToCommaSeparatedString(Collection
Parameter | Description |
---|---|
elementCollection | the element collection |
public static String collectionToCommaSeparatedString(Collection<String> elementCollection)
//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; } }