i have to arrays: arrA
and arrB
. arrA
and arrB
are Lists of objectss of diffrent types and add
function converts objects A
to objects B
. I want to add each object from arrA to arrB and remove that object from arrA. Im trying to do this by stream:
arrA.stream().foreach(c -> {arrB.add(c); arrA.remove(c);});
when i execute this, two things are happening:
- not all objects are passed from arrA to arrB.
- after few iterations null pointer exception is thrown.
i gues it's because length of array is decreased after each remove()
call and the counter of iterations is increased (only objects under odd indexes are passed to arrB
)
Now i could solve this by copying array in one stream call and then remove objects in second stream call but this doesnt seem correct for me.
What would be proper solution to this problem?
EDIT. Additional information: in real implementation this list if previously filtered
arrA.stream().filter(some condition).foreach(c -> {arrB.add(c); arrA.remove(c);});
and its called few times to add elements meeting diffrent conditions to diffrent lists (arrC, arrD
etc.) but each object can be only on one list