有没有更好的方法可以一次向集合中添加多个单词?
我只是想知道,一次将多个项目添加到 HashSet 的最佳方法是什么?
我正在做一项家庭作业,其中的目标是迭代 .java 文件并计算文件中的关键字。在作业描述的底部,它说(“提示:创建一个 Set 来保存所有 Java 关键字”)
我对 HashSet 并不完全熟悉,而且我不知道如何一次添加大量单词,而且我当然不想为每个关键字经历 .add("final") .add("true") ..等等。
因此,我创建了一个包含所有这些单词的数组列表。然后我使用 for 循环来循环并将每个元素添加到集合中。但是,这似乎是多余的。如果我已经在数组中获得了所有关键字,那么我不明白为什么需要将它们添加到 HashSet 中才能完成分配。 但是,为了更多地了解 HashSets,有没有一种方法可以在不使用我使用的方法(除了 1 by 1)的情况下做到这一点?
String[] aryKeywords = { "abstract", "asset", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictftp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "false", "null", "true" };
Set<String> jKeywords = new HashSet<String>();
for (int i = 0; i < aryKeywords.length; i++) {
jKeywords.add(aryKeywords[i]);
}
感谢您的任何见解!
I was just wondering, what is the best way to add several items to a HashSet at once?
I'm working on a homework assignment where the object is to iterate through a .java file and count the keywords in the file. At the bottom of the assignment description it says ("Hint: Create a Set to hold all Java keywords")
I'm not completely familiar with HashSets, and I didn't know how to add a bulk of words at once, and I certainly didn't want to go through .add("final") .add("true") ..and so on for each keyword.
So, I created an array list with all of those words. I then used a for loop to loop through and add each one to the set.However, this seems redundant. If I've got all the keywords in an array, then I don't see why I would need to add them to a HashSet in order to complete the assignment.
But, for sake of learning some more on HashSets, is there a way to do this without the method I used(other than 1 by 1)?
String[] aryKeywords = { "abstract", "asset", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictftp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "false", "null", "true" };
Set<String> jKeywords = new HashSet<String>();
for (int i = 0; i < aryKeywords.length; i++) {
jKeywords.add(aryKeywords[i]);
}
Thanks for any insight!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
乔恩·斯基特的回答是正确的。另一种(根据其文档,据说更快)方法是:
或者内联指定它们(以反映乔恩的答案):
Jon Skeet's answer is correct. Another (supposedly faster, according to its documentation) approach is:
Or to specify them inline (to mirror Jon's answer):
您可以使用:
或者如果您想内联指定它们:
如果您曾经遇到过已经得到一组的情况,则可以使用
addAll
:You could use:
Or if you want to specify them inline:
If you're ever in a situation where you've already got a set, you can use
addAll
:你可以这样做
,但这会增加更多的垃圾,以获得更干净的代码
you could do
but that adds more garbage for somewhat cleaner code
同样的事情有一些变体(双重 {{ 技巧,我不喜欢,但如果你真的想要我会提到),但是,Java 在这里并没有真正的完美答案。为你。另一种变体是您所做的 String[] ,然后是 Arrays.asList 将其放入列表中,然后是 set.addAll 。从效率的角度来看,没有一个是特别出色的。
There's some VARIANTS of the same thing (the double {{ trick, which I don't like, but if you really want I'll menion), but no, Java doesn't really have a perfect answer here. For you. Another variation is the String[] you did, followed by Arrays.asList it into a List and then set.addAll . From an efficiency POV, none are particularly great.
您可以使用 <代码>Arrays.asList():
You could use
Arrays.asList()
:我认为您不能绕过将项目表示为数组。但是,您可以使用类似的结构一次性添加它们
I don't think you can bypass representing you items as an array. But, you can add them all at once by using a construct like