Java 将逗号分隔的字符串转换为 HashSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19003055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:16:43  来源:igfitidea点击:

Convert comma separated string into a HashSet

javacsvhashset

提问by Menios

So, how would you go about converting

那么,您将如何进行转换

String csv = "11,00,33,66,44,33,22,00,11";

to a hashset in the quickest-most optimized way.

以最快、最优化的方式到哈希集。

This is for a list of user-ids.

这是用于用户 ID 的列表。

Update

更新

I ran all the answers provided through a test program where each method was called 500,000 times for a bigger CSV string. This test was performed 5 times continously (in case program startup slowed initial method) and I got the following in milliseconds (ms):

我运行了通过测试程序提供的所有答案,其中每个方法对于更大的 CSV 字符串被调用 500,000 次。这个测试连续执行了 5 次(以防程序启动减慢初始方法),我得到以下毫秒(ms):

Method One Liner->  6597
Method Split&Iterate->  6090
Method Tokenizer->  4306
------------------------------------------------
Method One Liner->  6321
Method Split&Iterate->  6012
Method Tokenizer->  4227
------------------------------------------------
Method One Liner->  6375
Method Split&Iterate->  5986
Method Tokenizer->  4340
------------------------------------------------
Method One Liner->  6283
Method Split&Iterate->  5974
Method Tokenizer->  4302
------------------------------------------------
Method One Liner->  6343
Method Split&Iterate->  5920
Method Tokenizer->  4227
------------------------------------------------


static void method0_oneLiner() {
        for (int j = 0; j < TEST_TIMES; j++) {
            Set<String> hashSet = new HashSet<String>(Arrays.asList(csv
                    .split(",")));
        }
    }

    // ———————————————————————————————–

    static void method1_splitAndIterate() {

        for (int j = 0; j < TEST_TIMES; j++) {
            String[] values = csv.split(",");
            HashSet<String> hSet = new HashSet<String>(values.length);
            for (int i = 0; i < values.length; i++)
                hSet.add(values[i]);
        }
    }

    static void method2_tokenizer() {

        for (int j = 0; j < TEST_TIMES; j++) {
            HashSet<String> hSet = new HashSet<String>();
            StringTokenizer st = new StringTokenizer(csv, ",");
            while (st.hasMoreTokens())
                hSet.add(st.nextToken());
        }
    }

采纳答案by Kayaman

The 6 other answers are great, in that they're the most straight-forward way of converting.

其他 6 个答案很棒,因为它们是最直接的转换方式。

However, since String.split()involves regexps, and Arrays.asListis doing redundant conversion, you might want to do it this way, which may improve performance somewhat.

但是,由于String.split()涉及正则表达式,并且Arrays.asList正在进行冗余转换,因此您可能希望这样做,这可能会在一定程度上提高性能。

Editif you have a general idea on how many items you will have, use the HashSetconstructor parameter to avoid unnecessary resizing/hashing :

编辑如果您对将拥有多少项目有一个大致的了解,请使用HashSet构造函数参数来避免不必要的调整大小/散列:

HashSet<String> myHashSet = new HashSet(500000);  // Or a more realistic size
StringTokenizer st = new StringTokenizer(csv, ",");
while(st.hasMoreTokens())
   myHashSet.add(st.nextToken());

回答by TheKojuEffect

String[] values = csv.split(",");
Set<String> hashSet = new HashSet<String>(Arrays.asList(values));

回答by Prabhakaran Ramaswamy

String[] array= csv.split(",");

Set<String> set = new HashSet<String>(Arrays.asList(array));

回答by Suresh Atta

You can try

你可以试试

Set<String> set= new HashSet<String>(Arrays.asList(yourString.split(",")));

回答by aUserHimself

Try this:

尝试这个:

Set<String> hashSet = new HashSet<>(Arrays.asList(csv.split(",")));

But be careful, this is maybe the easiest way to do it, but not necessarily the optimal.

但是要小心,这可能是最简单的方法,但不一定是最佳方法。

回答by newuser

try,

尝试,

String[] splitValues = csv.split(",");
Set<String> set = new HashSet<String>(Arrays.asList(splitValues));

and also use

并且还使用

CollectionUtils

集合实用程序

collectionutils.addall();

回答by sunysen

try

尝试

String[] args = csv.split(",");
Set<String> set = new HashSet<String>(Arrays.asList(args));

回答by SagarG

The current accepted answer by @Kayaman is good but I have something to add from the Java API webpage. I was unable to add this as a comment to the answer because of not having enough reputation.

@Kayaman 目前接受的答案很好,但我有一些东西要从 Java API 网页中添加。由于没有足够的声誉,我无法将其添加为答案的评论。

Use of StringTokenizer is discouraged. It is mentioned on the Java API webpage here http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

不鼓励使用 StringTokenizer。它在此处的 Java API 网页上提到http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它。建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包。

回答by dripp

Arrays.stream(csv.split(",")).collect(Collectors.toSet());