Get number of duplicates from ArrayList

前端 未结 3 378
自闭症患者
自闭症患者 2021-01-27 20:07

For example, say I have an ArrayList that could contain the following values:

x
x
x
y
y

Now what I want to retrieve is the number

3条回答
  •  天涯浪人
    2021-01-27 20:15

    Check out the Multiset in google guava: http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/collect/Multiset.html

    Example:

    Multiset strings = HashMultiset.create(arrayList);
    int countX = strings.count("x"); // the count of x
    

    More examples can be found on the Guava wiki.

提交回复
热议问题