Java: comparing two lists
4 answers
So, you want to make A = AU B. To do this, you can search every element of B in and add an element to A if the search fails. However, too many searches in the list are not recommended.
I would recommend using HashMap. Create a HashMap and loop through A and B, placing everything you see on the map. After that, you can convert the card back to a list.
[If you want the order to be the same as in the example, you must convert A to card. Then you have to go through B and for every failed map.get (element) file you have to add that element from B to A.]
0
source to share
Try the following:
import org.apache.commons.collections.CollectionUtils;
[...]
// Returns a Collection containing the exclusive disjunction (symmetric difference) of the given Collections.
Collection disJointList = CollectionUtils.disjunction(a, b);
//To check
for (Object object : disJointList) {
System.out.println(disJointList);
//output is {32, 952, 1835}
}
a.addAll( disJointList );
0
source to share