【问题标题】:Is there a better way to add several words to a Set at once?有没有更好的方法一次将多个单词添加到 Set 中?
【发布时间】:2011-04-26 05:54:54
【问题描述】:

我只是想知道,一次将多个项目添加到 HashSet 的最佳方法是什么?

我正在做一个家庭作业,其中对象是遍历 .java 文件并计算文件中的关键字。在分配描述的底部它说(“提示:创建一个集合来保存所有 Java 关键字”)

我对 HashSets 不是很熟悉,也不知道如何一次添加一大堆单词,当然也不想经过 .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]);
    }

感谢您的任何见解!

【问题讨论】:

  • 我猜这个作业需要你计算文件中的 distinct 关键字。将单词添加到 Set 是过滤重复项的简单方法;结果集大小是不同关键字的数量。
  • 好点。既然您已经提出了这一点,我可以看出这可能是意图。现在,我必须完成并编辑我的作业!我已经完成了对所有关键字的计数。 pastebin.com/m7kWzhie

标签: java hashset


【解决方案1】:

Jon Skeet 的回答是正确的。另一种(根据其文档,据说速度更快)方法是:

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

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

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

【讨论】:

  • 我认为除非我有证据表明这段代码的整体性能很重要,否则我可能会坚持使用asList - 它对我来说看起来更干净。值得了解Collections.addAll - 谢谢。
【解决方案2】:

你可以使用:

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));

【讨论】:

  • 更喜欢使用Collections.addAll(foo, bar) 而不是foo.addAll(Arrays.asList(bar))。前者的文档声称更快。
  • 感谢您的信息。我记得,尽管很模糊,几天前我在课堂上读过一些关于 Collections.addAll() 的东西。 --感谢所有回答的人。
【解决方案3】:

你可以这样做

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

但这会为更简洁的代码添加更多垃圾

【讨论】:

  • +1 用于警告生成垃圾。这通常不是一个重要的问题......但也不是避免循环:-)
【解决方案4】:

有一些相同事物的变体(双 {{ 技巧,我不喜欢,但如果你真的想要我会提及),但不,Java 在这里并没有真正的完美答案。为你。另一个变体是您所做的 String[] ,然后是 Arrays.asList 将其放入 List ,然后是 set.addAll 。从效率 POV 来看,没有一个是特别出色的。

【讨论】:

    【解决方案5】:

    你可以使用Arrays.asList():

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

    【讨论】:

      【解决方案6】:
      Set<String> jKeywords = new HashSet<String>(Arrays.asList(aryKeywords));
      

      【讨论】:

        【解决方案7】:

        我认为您不能绕过将项目表示为数组。但是,您可以使用

        之类的结构一次性添加它们
        new HashSet (Arrays.asList(aryKeywords));
        

        【讨论】:

          猜你喜欢
          • 2021-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-22
          • 2023-02-05
          • 1970-01-01
          • 2015-08-19
          • 2016-07-06
          相关资源
          最近更新 更多