Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the removed and added elements from a string arraylist

Tags:

java

arraylist

I have a String arraylist ListA containing values, say {a,b,c,d}. I did some operations and the ListA now contains, say, {b,c,e}

I have to find out what all elements have been removed from ListA i.e a and d and what all elements have been added i.e e.

Please help. I am new to Java and am stuck with this.

like image 512
user2358066 Avatar asked Feb 09 '26 20:02

user2358066


1 Answers

you actually have 3 options what i could suggest.

  1. have a referenceList from which you can compare the ListA after modification, so that you can list out added/removed elements.

    see Noob UnChained's answer for sample code.

  2. instead of using Arraylist, extend it and override remove() & Noob UnChained methods to add(make sure you persist a call to super() just in case) logic to store elements being removed/added.

    see Evgeniy Dorofeev's answer for sample code.

  3. instead of directly calling add()/remove() Write utility methods which will in turn call add()/remove() along with storing that element to add()/remove() from corresponding addedList & removedList

like image 176
Ankit Avatar answered Feb 15 '26 07:02

Ankit