File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes searching an index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "searching an index" Watch "searching an index" New topic

searching an index

Jurek Piasek

Joined: Jul 21, 2008
Posts: 2

I have a question about searching an index.

I have a set of dates and would like to search the set/index. However, I am not looking for an exact match, but rather all dates less than the given date i.e. if I have a date, say date0, I would like to do a search that returns all dates less than date0. Does java have a built in function that does this or do I have to write it myself?

Joe Ess

Joined: Oct 29, 2001
Posts: 8915

Welcome to the JavaRanch.
Java does not have a built in method to do what you require.
Take a stab at it and let us know if you have any problems. We'll be glad to help.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
You should be able to loop through the original list and use the Date compareTo() method to add the earlier dates to a separate list.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19697

If you use a SortedSet, you can use the headMap, subMap and tailMap methods.

How To Ask Questions How To Answer Questions
Jurek Piasek

Joined: Jul 21, 2008
Posts: 2
Many thanks for the quick responses. They were very helpful.

Although I didn't ask it, the index that I was looking to search was an index of a map. Using a SortedMap solves my problems.

Thanks again!
I agree. Here's the link:
subject: searching an index