Bạn đang thực sự yêu cầu cho Symmetric Difference.
List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
List<Integer> bList = new ArrayList<>(Arrays.asList(2, 3, 4, 6, 7));
// Union is all from both lists.
List<Integer> union = new ArrayList(aList);
union.addAll(bList);
// Intersection is only those in both.
List<Integer> intersection = new ArrayList(aList);
intersection.retainAll(bList);
// Symmetric difference is all except those in both.
List<Integer> symmetricDifference = new ArrayList(union);
symmetricDifference.removeAll(intersection);
System.out.println("aList: " + aList);
System.out.println("bList: " + bList);
System.out.println("union: " + union);
System.out.println("intersection: " + intersection);
System.out.println("**symmetricDifference: " + symmetricDifference+"**");
Prints:
aList: [1, 2, 3, 4]
bList: [2, 3, 4, 6, 7]
union: [1, 2, 3, 4, 2, 3, 4, 6, 7]
intersection: [2, 3, 4]
**symmetricDifference: [1, 6, 7]**
Hãy thử, sau đó chúng tôi sẽ giúp bạn. –
để bạn muốn xóa các phần tử phổ biến – SRy
Gợi ý: Nếu bạn đọc api cho Danh sách, bạn sẽ tự khắc phục sự cố của mình. – Sanchit