0

I have a list of 2D points (a custom Node class). I would like to pick a Node and say "Ehy, the 10 closest node in my list are those ten"

Now, what's the problem: I would like to make something like:

ArrayList<Node> nodes; //list of nodes
            ArrayList<Node> topten; //list of nearest nodes
            Node currentNode; //the current point
            for (Node x : nodes){
                if (x.distance(currentNode) < (topten.THE_FURTHER_POINT()).distance(currentNode))
                    topten.ADD(x);

            }

I would like to know if there is already a Collection which have those pseudo-methods (THE_FURTHER_POINT and ADD) Those two methods should work like this:

THE_FURTHER_POINT: return the further point. The collection should be fixed-length, so this method should always return the last element (if I have 8 elements inside the topten, i should receive the node with index 7, if I have 10 elements I should receive the node with index 9)

ADD: everytime an elements is added, if the collection is full the latest elements get eliminated. The collection should be always ordinate, and this operation should be as quick as possibile (if I have 8 elements in the topten, I will end up with 9 elements after the add. if I have 10 elements, I will end up with 10 elements after the add)

Thank you a lot!

  • Create your custom List class by implementing `List` interface or depending on your need extend `ArrayList` class – Courage Apr 29 '17 at 14:06

1 Answers1

0

In the end, what i really was searching for was a collection

  • ALWAYS SORTED
  • WITH FIXED LENGTH

So... After reading Java PriorityQueue with fixed size I think I found my reply. This can be considered a duplicate question.

Community
  • 1
  • 1
  • 2
    When you understand that your question is a dupe, then don't write an answer. Vote to flag as a dupe (like you already did, thank you) and then wait. When someone else also votes for duplication, then you should be able to accept that (you get a notification on top of the question) and the question will be closed. – Tom Apr 29 '17 at 14:24