aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "searching an index" Watch "searching an index" New topic
Author

searching an index

Jurek Piasek
Greenhorn

Joined: Jul 21, 2008
Posts: 2
Hi,

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?

Thanks,
Jurek.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jurek Piasek
Greenhorn

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!
 
Consider Paul's rocket mass heater.
 
subject: searching an index