0

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.

Warringer
  • 3
  • 2
  • Could [this](http://stackoverflow.com/questions/1963806/is-there-a-fix-sized-queue-which-removes-excessive-elemets) answer be helpful? – max.shmidov Apr 04 '12 at 18:12
  • 1
    This should help you: http://stackoverflow.com/questions/1963806/is-there-a-fix-sized-queue-which-removes-excessive-elemets – ahanin Apr 04 '12 at 18:15
  • I already tried that, but it does not work in the way I want it to work. – Warringer Apr 04 '12 at 18:18

3 Answers3

2

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 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 Apr 04 '12 at 18:23
1

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

gnomed
  • 5,483
  • 2
  • 26
  • 28
  • 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 Apr 04 '12 at 18:20
  • @Warringer - my misunderstanding. Oh well, queue was probably best anyway – gnomed Apr 05 '12 at 16:37
0

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.

trutheality
  • 23,114
  • 6
  • 54
  • 68