arrays - 两个Java数组的对称差异

标签 arrays java

我有两个数组

 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/

相关文章:

Java: boolean 值+整数

javascript - 使用 array.map、promises 和 setTimeout 更新数组

arrays - 掉落(其中 : {}) remove(where: {}) function in Swift?

java - GWT。在不覆盖原生样式的情况下向元素添加主要的 css 样式

java - log4j2 xml配置日志到2个文件

java - 将日期和时间与可接受的差异进行比较

C++ 数组、For 循环和字符串数据类型

具有额外状态的 Ruby 数组

c - 将数组传递给 scanf 函数将不起作用

java - Wicket:使(启用 CDI 的)页面可序列化时要记住什么?