Java Comma Separated List loadListFromStrings(final String commaDelimitedStrings, final boolean sortList)

Here you can find the source of loadListFromStrings(final String commaDelimitedStrings, final boolean sortList)

Description

Converts a string containing a comma-delimited list of values into a List containing those values.

License

Apache License

Parameter

Parameter Description
commaDelimitedStrings A string containing a comma-delimited list of values.
sortList If this is <code>true</code> then the returned list will be sorted.

Return

A List containing the separated values.

Declaration

public static ArrayList<String> loadListFromStrings(final String commaDelimitedStrings,
        final boolean sortList) 

Method Source Code

//package com.java2s;
/*/*from  www .  j a va  2s  .  c o m*/
 * Copyright 2006-2014 Andy King (GreatLogic.com)
 * 
 * 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.ArrayList;
import java.util.Collections;

public class Main {
    /**
     * Converts a string containing a comma-delimited list of values into a <code>List</code> containing
     * those values.
     * @param commaDelimitedStrings A string containing a comma-delimited list of values.
     * @param sortList If this is <code>true</code> then the returned list will be sorted.
     * @return A <code>List</code> containing the separated values.
     */
    public static ArrayList<String> loadListFromStrings(final String commaDelimitedStrings,
            final boolean sortList) {
        return loadListFromStrings(null, commaDelimitedStrings, sortList);
    }

    /**
     * Converts a string containing a comma-delimited list of values into a <code>List</code> containing
     * those values.
     * @param stringList The <code>List</code> into which the values will be placed. If this is
     * <code>null</code> then a new <code>List</code> will be created.
     * @param commaDelimitedStrings A string containing a comma-delimited list of values.
     * @param sortList If this is <code>true</code> then the returned list will be sorted.
     * @return A <code>List</code> containing the separated values.
     */
    public static ArrayList<String> loadListFromStrings(final ArrayList<String> stringList,
            final String commaDelimitedStrings, final boolean sortList) {
        final ArrayList<String> result = stringList == null ? new ArrayList<String>(20) : stringList;
        try {
            final String[] strings = commaDelimitedStrings.split(",");
            result.clear();
            Collections.addAll(result, strings);
            if (sortList) {
                Collections.sort(result);
            }
        } catch (final Exception e) {
            // the list will have everything up to the first exception
        }
        return result;
    }
}

Related

  1. listToCommas(List list)
  2. listToCommaSep(List strings)
  3. listToCommaSeparatedList(String prefix, List stringList)
  4. listToCommaSeparatedString(List list)
  5. listToString(List list, boolean doCommas)
  6. moveClasspathArgToEnvironmentVariable(List commandArgs, ProcessBuilder pb)
  7. parseCommaList(String nameCommaList)
  8. parseRsfCommands(List rsfLines, List addItemCommands, List appendEntryCommands)
  9. resolveCommandParam(List params, String name, double defaultValue)