I have an Object class : DemoObject.
DemoObject is like a list of some fooObjects.
fooObjects have a field called as “ID”.
Now suppose I have two demoObject : demoObject1 and demoObject2. Now i want to check if there are some fooObject present in both demoObjects i.e in demoObject1 as well as in 2 then I probably removed it from demoObject1.
For checking fooObject from both demoObject I check with if fooObject “ID” field is same or not.
How I can accomplish it most effeciently.
The simplest and most idiomatic solution requires you to provide an
equals()method in classFooObjectwhich compares the objects based on their ID (or an appropriateComparator).Once you have this, just put the first list into a
Setand useSet.removeAllto remove from the set all of its elements that are contained in list 2: