Search code examples
javalistreplaceduplicatesinsertion

Structure that allows duplicates, maintains insertion order and allows removal and insertion


I am looking for a data structure that allows duplicates and maintains insertion order so that if given file input of : a + a + b = c

So that once correctly split, I will get: {a,+,a,+,b,=,c}

This data structure also needs to allow for removal and insertion in the correct order, for example if I replace a with d, I should get {d,+,d,+,b,=,c}.

Finally the structure must also be able to recognize which items are before or after a certain item. E.g. the item directly before = is b and directly after is c.

I am aware that lists allow duplicates and some lists maintain insertion order, but I am unsure as to which will allow me to achieve my goals.

If you are aware of a structure the will achieve all of the above, please provide the syntax for creating such a structure.

Regards


Solution

  • Seems ArrayList with ListIterator would fulfill your requirements, for sample usage, see: http://www.java2s.com/Code/Java/Collections-Data-Structure/BidirectionalTraversalwithListIterator.htm