Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Collections - Effienct search for DateTime ranges

I have a case where I have a table (t1) which contains items like

 | id | timestamp | att1 | att2 |

Now I have to iterate over a collection of elements of type att1 and get all records from t1 which are between two certain timestamps for this att1. I have to do this operation several times for a single att1.

So in order to go easy on the database queries, I intended to load every entry from t1 which has a certain att1 attribute once into a collection and perform the subsequent searches on this collection.

Is there a collection that could handle a search like between '2011-02-06 09:00:00' and '2011-02-06 09:00:30'? It's not guaranteed to contain entries for those two timestamps.

Before writing an implementation for that (most likely a very slow implementation ^^) I wanted to ask you guys if there might be some existing collections already or how I could tackle this problem.

Thanks!

like image 836
chzbrgla Avatar asked May 02 '26 16:05

chzbrgla


1 Answers

Yes. Use TreeMap which is basically a sorted map of key=>value pairs and its method TreeMap::subMap(fromKey, toKey).

In your case you would use timestamps as keys to the map and for values att1 attribute or id or whatever else would be most convenient for you.

like image 127
pajton Avatar answered May 05 '26 06:05

pajton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!