How can I know the different element between 2 array list in java? I need the exact element not a Boolean value which can be retrieved using removeAll()
.
If I understood your question correctly then following method nonOverLap
in the code below should get you that:
Collection union(Collection coll1, Collection coll2) {
Set union = new HashSet<>(coll1);
union.addAll(new HashSet<>(coll2));
return union;
}
Collection intersect(Collection coll1, Collection coll2) {
Set intersection = new HashSet<>(coll1);
intersection.retainAll(new HashSet<>(coll2));
return intersection;
}
Collection nonOverLap(Collection coll1, Collection coll2) {
Collection result = union(coll1, coll2);
result.removeAll(intersect(coll1, coll2));
return result;
}