PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] How do I have the nearest element to a certain element in a set?

 November 07, 2022     algorithm, java, set, sorting     No comments   

Issue

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.


Solution

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.



Answered By - erickson
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing