I am looking for a data structure that:
I ruled out ArrayList
because it isn't efficient at inserting at the beginning of the list.
On the surface LinkedList
should be a perfect match, but in practice the Java implementation isn't efficient at inserting before or after existing elements (i.e. it walks the entire list to find the insertion position).
(I don't personally need to store duplicate elements but others might)
Motivation: I am building an event queue that allows occasional cheating (inserting before or after an existing event).
Honestly I think a custom implementation of a LinkedList
would be the way to go:
Map<?, Node>
when inserting/removing elements, then you can access a Node
(and its previous/next Nodes) in constant time and insert/remove that way.Depending on the total amount of events (and how often events can cheat), linear time could be considered as well, allowing you to use the API's implementation of LinkedList
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With