有没有更好的方法可以一次向集合中添加多个单词?

发布于 2024-11-03 11:38:56 字数 1158 浏览 2 评论 0原文

我只是想知道,一次将多个项目添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

叹梦 2024-11-10 11:38:56

乔恩·斯基特的回答是正确的。另一种(根据其文档,据说更快)方法是:

Set<String> jKeywords = new HashSet<String>();
Collections.addAll(jKeywords, aryKeywords);

或者内联指定它们(以反映乔恩的答案):

Collections.addAll(jKeywords, "abstract", "asset", "boolean", /* ... */);

Jon Skeet's answer is correct. Another (supposedly faster, according to its documentation) approach is:

Set<String> jKeywords = new HashSet<String>();
Collections.addAll(jKeywords, aryKeywords);

Or to specify them inline (to mirror Jon's answer):

Collections.addAll(jKeywords, "abstract", "asset", "boolean", /* ... */);
固执像三岁 2024-11-10 11:38:56

您可以使用:

Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));

或者如果您想内联指定它们:

Set<String> jKeywords = new HashSet<String>(Arrays.asList(
    "abstract", "asset", "boolean", /* etc */));

如果您曾经遇到过已经得到一组的情况,则可以使用addAll

jKeywords.addAll(Arrays.asList(extraMembers));

You could use:

Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));

Or if you want to specify them inline:

Set<String> jKeywords = new HashSet<String>(Arrays.asList(
    "abstract", "asset", "boolean", /* etc */));

If you're ever in a situation where you've already got a set, you can use addAll:

jKeywords.addAll(Arrays.asList(extraMembers));
相思碎 2024-11-10 11:38:56

你可以这样做

jKeywords.addAll(Arrays.<String>asList(aryKeywords));

,但这会增加更多的垃圾,以获得更干净的代码

you could do

jKeywords.addAll(Arrays.<String>asList(aryKeywords));

but that adds more garbage for somewhat cleaner code

怎樣才叫好 2024-11-10 11:38:56

同样的事情有一些变体(双重 {{ 技巧,我不喜欢,但如果你真的想要我会提到),但是,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.

深海蓝天 2024-11-10 11:38:56

您可以使用 <代码>Arrays.asList()

Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));

You could use Arrays.asList():

Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
无妨# 2024-11-10 11:38:56
Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
陪我终i 2024-11-10 11:38:56

我认为您不能绕过将项目表示为数组。但是,您可以使用类似的结构一次性添加它们

new HashSet (Arrays.asList(aryKeywords));

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

new HashSet (Arrays.asList(aryKeywords));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文