java 将大字符串拆分为 SET 项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11387844/
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-10-31 04:53:34  来源:igfitidea点击:

splitting a large string into SET items

javaset

提问by Achilles

Possible Duplicate:
Java - easily convert array to set

可能的重复:
Java - 轻松地将数组转换为集合

Can someone help me with a version of the following expression that I can use for SET instead of ArrayList ?

有人可以帮助我使用以下表达式的一个版本,我可以将其用于 SET 而不是 ArrayList 吗?

ArrayList<String> items = new ArrayList<String>(Arrays.asList(comment.split(", ")));

P.S.: Comment is a large string of words split with a ",". Need to make an individual item of the word by splitting them from the comma sections.

PS:评论是一大串单词,用",". 需要通过将它们从逗号部分分开来制作单词的单个项目。

回答by Konrad Reiche

You use the same approach, just passing the converted array to the constructor of a Setimplementation:

您使用相同的方法,只需将转换后的数组传递给Set实现的构造函数:

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

Further simplification are not possible without third-party libraries, but there are no drawbacks, since Arrays.asListexecutes in constant time O(1).

如果没有第三方库,进一步简化是不可能的,但没有缺点,因为Arrays.asList在恒定时间O(1) 内执行