palyxk palyxk - 1 month ago 11
Java Question

Comparing values in ArrayList

How to compare objects in 2 distinct lists?
I have two list List#1 and List#2 in each there could be 1 or more objects i need to transport all changes from #1 to #2. So they would be the same nevertheless the changes from #2. It should work only from #1 -> #2. List(s) may not be ordered and could contain different values for object therefore I canĀ“t order by id. I need to find which object from #1 is comparable to object from #2 and then copy only changes from #1 to #2.

Answer

From what I think you are trying to do from your question (with No code). You are trying to populate List #2 with content of List #1 and you dont want duplicate data.

You can do one thing if you want to add content of List 1 and List 2 in one list. Use a HashSet. HashSet doesn't allow duplication of Data. So, once your List1 is updated in HashSet, iterate through List2 and populate the HashSet with content of List2.

Result- You will get a hashSet of Unique data from both List1 and List2

You can use this Tutorial - http://www.javatpoint.com/HashSet-class-in-collection-framework