I know that the title doesn't really say what I'm actually looking for, as its hard to explain in few words.
What I'm looking for is a Linked List variant for Java I can iterate over, but which has something of a fixed length in a way.
You see, I want to track a ground path of simulated satellite in Kerbal Space Program, with data I get from a Telemetry Plugin. But I only want to display the ground path over the last about two hours. Now the entire data would be written into the Linked List, but as time goes by the list grows longer and longer and eventually its so large that it takes longer to iterate over this list to get the data of the last two hours of the orbit then it takes for a new set of data to come in.
So the Linked List variant I'm looking for would be of a somewhat fixed length that deletes the last entry(entries) if the time between the eldest and the newest entry are over two hours mission time. So that I only have to iterate over a relatively low number of entries and not the entire dataset of the previous flight (which is saved to bump it to CSV).
I appreciate any help that may be rendered by the helpful people around here.
Just maintain a thread that periodically trims the end of the LinkedList
. You don't need anything special for this. Any Queue
implementation would probably work; ArrayDeque
might be best.
That, or decorate a LinkedList
with a wrapper Queue
implementation that throws away elements that are too old.
I would recommend either using the queue as suggested by Louis Wasserman. But the other possibility you might want to consider is a circular linked list.
This is simply the first data structure that came into my mind as you were describing the problem and I think it fits most naturally. Unfortunately I am not aware of any native circular linked list implementations available from Java, so you would have to implement your own or use 3rd party code if you did this...
I would suggest wrapping almost any list implementation, when you add to the list, compare the element you just added to the last element in the list, and remove the last element if the time difference is bigger than two hours.