Seeking LinkedList variant of fixed length, sort of

Go To


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.

2012-04-04 18:08
by Warringer
Could this answer be helpful - max.shmidov 2012-04-04 18:12
This should help you: - ahanin 2012-04-04 18:15
I already tried that, but it does not work in the way I want it to work - Warringer 2012-04-04 18:18


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.

2012-04-04 18:09
by Louis Wasserman
Hmmm... The idea sounds good. But since I'm not all that good with threads and using them, I think I will try to use something that automatically looks if it can trim the last entry if a new entry is added - Warringer 2012-04-04 18:23


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...

2012-04-04 18:14
by gnomed
The Linkd list I'm looking for would be dynamic in length, through over a 'fixed duration' as there are not always the same number of entries over the same duration - Warringer 2012-04-04 18:20
@Warringer - my misunderstanding. Oh well, queue was probably best anywa - gnomed 2012-04-05 16:37


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.

2012-04-04 18:20
by trutheality
I got that myself from the first answer, but thank you anyway. : - Warringer 2012-04-04 19:28