This week's book giveaway is in the Open Source forum.
We're giving away four copies of RabbitMQ in Depth and have Gavin Roy on-line!
See this thread for details.
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

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
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: 9101

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.

[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: 19990

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