Java Collection Split splitToCollection(String list, String separator, Collection dest)

Here you can find the source of splitToCollection(String list, String separator, Collection dest)

Description

Split the given String on a separator and add the (trimmed) results to a given Collection

License

Apache License

Parameter

Parameter Description
list a parameter
separator a parameter
dest a parameter

Declaration

public static Collection<String> splitToCollection(String list, String separator, Collection<String> dest) 

Method Source Code

//package com.java2s;
/**//from  w w w. java2  s. com
 *  Copyright 2016 Polydeuce-Sys Ltd
 *
 *       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.
 **/

import java.util.Collection;

public class Main {
    private static final String DEFAULT_SEPARATOR = ",";

    /**
     * Split the given String on a separator and add the (trimmed) results to a given {@link Collection}
     * @param list
     * @param separator
     * @param dest
     * @return
     */
    public static Collection<String> splitToCollection(String list, String separator, Collection<String> dest) {
        String[] parts = list.split(separator);
        for (String s : parts) {
            if (!s.equals("")) {
                dest.add(s.trim());
            }
        }
        return dest;
    }

    /**
     * Split a comma separated String and put the results in the given {@link Collection}
     * @param list
     * @param dest
     * @return
     */
    public static Collection<String> splitToCollection(String list, Collection<String> dest) {
        return splitToCollection(list, DEFAULT_SEPARATOR, dest);
    }
}

Related

  1. split(Collection orig, int batchSize)
  2. split(Collection set, int n)
  3. split(final Collection collection)
  4. splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection into)
  5. splitToCollection(Collection collection, String valueString)
  6. splitToCollection(String str, String sep, Collection c)