我有两个数组
String[] ID1={"19","20","12","13","14"};
String[] ID2={"10","11","12","13","15"};
我如何在比较以上两个数组时得到以下答案。
我想在比较以上两个数组时排除公共(public)元素。
String[] Result={"14","15","19","20","10","11"};
最佳答案
似乎您想要两个集合的并集(没有重复,对吧?)减去交集:
Set<Integer> union = new HashSet<Integer>(Arrays.asList(ID1));
union.addAll(Arrays.asList(ID2);
Set<Integer> intersection = new HashSet<Integer>(Arrays.asList(ID1));
intersection.retainAll(Arrays.asList(ID2);
union.removeAll(intersection);
// result is left in "union" (which is badly named now)
(我将您的字符串更改为整数,这似乎更适合数据,但它会以相同的方式处理字符串)
关于arrays - 两个Java数组的对称差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762164/