I'm having a hash table with date object as key , the purpose is I can pick the value which is available in given date range. Consider the below example
Coffee Powder[Code CODE1] - Effective 21-Jan & Expiry 20-Jun
Coffee Powder[Code CODE1] - Effective 22-Jun & Expiry 30-Dec
Meat[Code CODE2] - Effective 21 - Jan & Expiry 1 - Feb
Meat[Code CODE2] - Effective 2 Feb & Expiry 6 - Feb
Currently I'm using the following hashtable & it has the structure Key - Code & Value [ List of objects] ]
CODE1- List[ Coffe Powder - 21 Jan - 20 Jun , Coffee Powder - 22 Jun - 30 Dec ]
CODE2- List[ Meat - 21 Jan - 01 Feb, Meat - 02 Feb - 06 Feb ]
To get the value which is valid for the given date , i need to get the list by passing the code & iterate the list to get the best matched value.
Example , if I want the Coffee Powder Information which is valid on 1 Feb. I need to get pass the code ( CODE 1 ) which will returns the list of objects. After we need to iterate the list to get the best match.
Instead of iteration , is there any way to maintain the hashtable & get the results quicker..?
To get a benefit out of the HashMaps quick access to keys you you'd need an exact match between your search date and a key date.
Can you cluster your Map-Content? eg.: do you always serch for a complete month?
Then you could change your Map from Map<Date,Object> to Map<Date,List<Object>> where Date is "normalised" to the first Day in month 00:00 o clock (UTC or local...).
You could also consider an additional map (Map<Date,List<Date>>) where you group the actual keys of the original Map by the expected search clusters.
You can also consider putting the data in a database table (which I prefer) as Date objects and querying what you want with SQL, something like the following pseudo code, where '?' is today's date.
select ItemName from Stock where ?>=EffectiveDate and ?<=ExpireDate order by ItemName