I have 2 ArrayList, which are periodically filled with data according to the following principle:

The first launch: ArrayList1 contains 3 numbers: 111, 222, 333; ArrayList2 contains 3 numbers: 111, 222, 333; ArrayList1 is cleared; ArrayList1 gets all the values ​​of ArrayList2; ArrayList2 is cleared;

The second launch: ArrayList1 contains: 111, 222, 333; ArrayList2 contains: 444, 111, 222; ArrayList1 is cleared; ArrayList1 gets all the values ​​of ArrayList2; ArrayList2 is cleared;

The third launch: ArrayList1 contains: 444, 111, 222; ArrayList2 contains: 555, 444, 111; ArrayList1 is cleared; ArrayList1 gets all the values ​​of ArrayList2; ArrayList2 is cleared.

And so on. That is, new values ​​periodically appear and "shift" the old data. The question is: how do you add these new values ​​to ArrayList3 each time? That is, add a check: if new values ​​have appeared (their number is not known in advance), then enter them into ArrayList3.

  • one
    any List has a contains() method that returns true if the object is in the list. Using this method, you can check if the object is already in the list. - Vladyslav Matviienko

1 answer 1

Maybe you use Set? If the objects are unique, then

 Set s2 = ArrayList2.stream().collect(Collectors.toSet()); Set s1 = ArrayList1.stream().collect(Collectors.toSet()); s2.removeAll(s1); 

And what's left in s2 add to ArrayList3