code,以确定没有什么不同的项目两个数组列表 [英] Code to determine no of different items in two arraylist
问题描述
我有被定义为遵循两个数组列表。
I have two arraylist which is defined as follows .
ArrayList<String> list1 = new arrayList<String>();
ArrayList<String> list2 = new arrayList<String>();
list1.add("sultan");
list1.add("Masum");
list1.add("sultan");
list1.add("Sorry");
list1.add("sultan");
list1.add("Masum");
list1.add("sultan");
list1.add("Tarek");
list1.add("sultan");
list2.add("Billal");
list2.add("Masum");
list2.add("Sorry");
list2.add("Sorry");
list2.add("sultan");
list2.add("Masum");
list2.add("Tanha");
list2.add("Tarek");
list2.add("Die");
我想知道没有上述两个ArrayList的不同项目。为此,我ahve下面这段code的。
I want to know the no of different items of the above two arraylist . For this I ahve the following piece of code .
int count=0;
for(String s1:list1)
{
if(!list2.contains(s1))
++count;
}
for(String s2:list2)
{
if(!list1.contains(s2))
++count;
}
但我的code是非常的高效。你可以请建议我一个更好的方法来达到我的目的?
But my code is very in-efficient . Can you please suggest me a better way to achieve my purpose ?
推荐答案
而不是列表
你应该使用设置
。那么你要寻找的是对称差。如果允许,可以用番石榴的<一个href=\"http://docs.guava-libraries.google$c$c.com/git/javadoc/com/google/common/collect/Sets.html#symmetricDifference%28java.util.Set,%20java.util.Set%29\"相对=nofollow> Sets.symmetricDifference(套装,套装)
Instead of List
you should use Set
. Then what you're looking for is Symmetric Difference. If allowed, you can use Guava's Sets.symmetricDifference(Set, Set)
这篇关于code,以确定没有什么不同的项目两个数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!