类别1:如何比较两个集合并获取它们之间的匹配数量?集合还包含重复
{ "tom", "dick", "harry", "john", "smith", "dick" };
系列2:
{ "Rachel", "dick", "tom", "dick", "smith","harry","Orion"}
的重复应计数两次。
类别1:如何比较两个集合并获取它们之间的匹配数量?集合还包含重复
{ "tom", "dick", "harry", "john", "smith", "dick" };
系列2:
{ "Rachel", "dick", "tom", "dick", "smith","harry","Orion"}
的重复应计数两次。
你可以只是做一个交集,并得到结果集的大小:
Set<String> s1 = new HashSet<String>(Arrays.asList(new String[] {"tom", "dick", "harry", "john", "smith", "dick"}));
Set<String> s2 = new HashSet<String>(Arrays.asList(new String[] {"Rachel", "dick", "tom", "dick", "smith", "harry", "Orion"}));
s1.retainAll(s2); // s1 becomes the intersection of s1 & s2
System.out.println("Number of common elements: " + s1.size());
[编辑]应该有更多的仔细阅读的问题。将上面的Set
替换为Apache Commons中的Bag实现,它也适用于重复项。
Set
实现在retainAll
调用后给出了s1的大小4,而Bag
版本的大小为5,我相信这是你想要的。
1.使用Collections.frequency()
方法来检查特定字符串对象在集合中发生的次数。
2.现在,您可以在其他集合每个对象,如果任一对象的频率数在另一个集合为零,其明显的是,这个集合不具有该对象的检查频率,所以这些集合是不相等的。
如果第一个集合中的所有对象都存在于第2话,尝试检查第2集的对象以相同的方式为我点提到的2
[你尝试过什么? ](http://www.whathaveyoutried.com/) –
如何处理dup?数他们两次?一旦? – elyashiv
它是什么类型的集合,你可以请指定..字符串,IEnumerable,JSON等? –