【问题标题】:how to compare 2 set in java containing characters in it?如何比较Java中包含字符的2个集合?
【发布时间】:2015-12-03 01:10:01
【问题描述】:

我有两套:

Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();

set1 包含[txt file]set2 包含[a,e,i,o,u]

我想比较这两个集合并在文本文件中找到元音的数量。

【问题讨论】:

  • 我们是否必须从文本文件中提取每个字符并与 set2 进行比较?如果不是,那么我们如何将 char 与字符串进行比较?

标签: java char set compare


【解决方案1】:
  1. 使用 guava

    Sets.intersection(set1, set2).size()

  2. 使用 apache commons 集合

    CollectionUtils.intersection(collection1, collection2).size()

  3. 使用 gs-collections

    mutableSet1.intersect(mutableSet2).size()

  4. 使用规划JDK

    设置结果 = new HashSet(set1);

    result.retainAll(set2);

    结果.size();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2021-06-06
    • 2020-02-28
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多