集册 Java实例教程 将CSV字符串列表转换为集合的方便方法。

将CSV字符串列表转换为集合的方便方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

629
将CSV字符串列表转换为集合的便捷方法。
/**
来 自 nowjava.com
**/


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.Collections;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Hashtable;

import java.util.List;

import java.util.Locale;

import java.util.Map;

import java.util.Properties;

import java.util.Scanner;

import java.util.Set;

import java.util.StringTokenizer;

import java.util.TreeSet;


public class Main{

    public static void main(String[] argv) throws Exception{

        String str = "nowjava.com";/** nowjava - 时代Java 提供 **/

        System.out.println(commaDelimitedListToSet(str));

    }

    /**

     * Convenience method to convert a CSV string list to a set. Note that this will suppress duplicates.

     *

     * @param str

     *            the input String

     * @return a Set of String entries in the list

     */

    public static Set<String> commaDelimitedListToSet(final String str) {


        final Set<String> set = new TreeSet<String>();

        final String[] tokens = commaDelimitedListToStringArray(str);

        for (final String token : tokens) {

            set.add(token);

        }

        return set;

    }

    /**

     * Convert a CSV list into an array of Strings.

     *

     * @param str

     *            the input String

     * @return an array of Strings, or the empty array in case of empty input

     */

    public static String[] commaDelimitedListToStringArray(final String str) {


        return delimitedListToStringArray(str, ",");

    }

    /**

     * Take a String which is a delimited list and convert it to a String array. <p>A single delimiter can

     * consists of more than one character: It will still be considered as single delimiter string, rather

     * than as bunch of potential delimiter characters - in contrast to <code>tokenizeToStringArray</code>.

     *

     * @param str

     *            the input String

     * @param delimiter

     *            the delimiter between elements (this is a single delimiter, rather than a bunch individual

     *            delimiter characters)

     * @return an array of the tokens in the list

     * @see #tokenizeToStringArray

     */

    public static String[] delimitedListToStringArray(final String str,

            final String delimiter) {


        return delimitedListToStringArray(str, delimiter, null);

    }

    /**

     * Take a String which is a delimited list and convert it to a String array. <p>A single delimiter can

     * consists of more than one character: It will still be considered as single delimiter string, rather

     * than as bunch of potential delimiter characters - in contrast to <code>tokenizeToStringArray</code>.

     *

     * @param str

     *            the input String

     * @param delimiter

     *            the delimiter between elements (this is a single delimiter, rather than a bunch individual

     *            delimiter characters)

     * @param charsToDelete

     *            a set of characters to delete. Useful for deleting unwanted line breaks: e.g. "\r\n\f" will

     *            delete all new lines and line feeds in a String.

     * @return an array of the tokens in the list

     * @see #tokenizeToStringArray

     */

    public static String[] delimitedListToStringArray(final String str,

            final String delimiter, final String charsToDelete) {


        if (str == null)

            return new String[0];

        if (delimiter == null)

            return new String[] { str };

        final List<String> result = new ArrayList<String>();

        if ("".equals(delimiter)) {

            for (int i = 0; i < str.length(); i++) {

                result.add(deleteAny(str.substring(i, i + 1), charsToDelete));

            }

        } else {

            int pos = 0;

            int delPos;

            while ((delPos = str.indexOf(delimiter, pos)) != -1) {

                result.add(deleteAny(str.substring(pos, delPos),

                        charsToDelete));

                pos = delPos + delimiter.length();

            }

            if ((str.length() > 0) && (pos <= str.length())) {

                // Add rest of String, but not in case of empty input.

                result.add(deleteAny(str.substring(pos), charsToDelete));

            }

        }

        return toStringArray(result);

    }

    /**

     * Delete any character in a given String.

     *

     * @param inString

     *            the original String

     * @param charsToDelete

     *            a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.

     * @return the resulting String

     */

    public static String deleteAny(final String inString,

            final String charsToDelete) {


        if (!hasLength(inString) || !hasLength(charsToDelete))

            return inString;

        final StringBuilder sb = new StringBuilder();

        for (int i = 0; i < inString.length(); i++) {

            final char c = inString.charAt(i);

            if (charsToDelete.indexOf(c) == -1) {

                sb.append(c);

            }

        }

        return sb.toString();

    }

    
展开阅读全文