两个数组之间的差异
问题描述:
可能的重复:
java中数组列表的交集/并集
您好,我有两个字符串数组.我想打印两个数组之间的差异.有没有针对此的 Java 方法?例如;
Hello I have two string arrays.I want to print differences between two arrrays.Is there any java method for this? For example;
String[ ] first={"A","B","C"};
String[ ] second={"C","B"};
并且结果必须是A".感谢所有评论.
and result must be "A". Thanks for all comments.
答
将数组转换为 Set
new HashSet<String>(Arrays.asList(array));
然后做
Set<String> commonOnes = biggerSet.retainAll(smallerSet);
biggerSet.removeAll(commonOnes).add(smallerSet.removeAll(commonOnes))
或使用番石榴 difference()
or use guava difference()