Home > Software engineering >  How do I have the nearest element to a certain element in a set?
How do I have the nearest element to a certain element in a set?

Time:10-02

Let say that I have a set as so:

Set<DueDate> dueDates;

class DueDate{
     Date date;
}

I need to find the nearest date element that comes right after a specified date.

The set contains around a 1000 entries. Is it ok if I just iterate over the set to find the nearest date entry or I have to use some type of sort or data structure to do it in a more efficient way.

CodePudding user response:

TreeSet is a NavigableSet implementation. Your requirements are not precise, so I'm not sure if you are looking for the higher() (strictly greater) or ceiling() (greater or equal) method, but one of those should meet your needs.

  • Related