Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA
Accepted Solution

Iterate through hashtable of dates

Hello.  I have a hashtable on dates/numbers (string/integer).  I need to loop throught this hashtable and get the sum of the values for all keys that fall between 2 dates.  Not sure how I am going to approach this.  What is an efficient way?  I'm thinking something like the following however not sure that this is the right approach...

 

 

Enumeration keys = theHashTable.keys();

Enumeration elements =theHashTable.elements();

String key;

Integer value;

while(keys.hasMoreElements()) {

key = (String)keys.nextElement();

value = (Integer)elements.nextElement();

//somewhere/somehow cast string date as long for compare/check if this key is between a date range and if so sum up the values

}

 

Any suggestions on an efficient method of doing this?

 

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Iterate through hashtable of dates

Hi @carll70

 

1. If this is an on going task which happens lots of times, consider using a different data structure.

 

2. If you still have to use a HashTable, you can enumerate the keys (and keys only) and then add your objects to a SimpleSortingVector (http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/util/SimpleSortingVector.html) and then sum the values according to the keys.

 

Note: when iterating the keys, you should take the values from the HashTable and NOT from the Enumeration.

Hope that helps,

 

E.

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: Iterate through hashtable of dates

Thanks for the reply.  I do need to use a hashtable.  Considered sqlite database but an sd card may not always be available.  Also thinking maybe this might be easier if I use a longhashtable and stores dates as long.  Not sure its going to matter much.  How about the date comparison from the string value?  Need to know if the date is between 2 dates before I sum up the values.  Any thoughts on what might be the most efficient way to do this?

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Iterate through hashtable of dates

You can try and convert a Date in String to a Date object using DateFormat (or its sons).

 

Once you have a Date object you can use the Calendar API (http://www.blackberry.com/developers/docs/5.0.0api/java/util/Calendar.html) to compare the dates.

 

Hope that helps,

 

E.

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: Iterate through hashtable of dates

Thank you.  Maybe if I use this approach I can iterate through each key and pass it off for for conversion and evaluation, if it's between the dates I require then I can sum the values.  May not be a need for simplesortingvector.  Figure this is effiicient enough/workable?

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Iterate through hashtable of dates

This approach is indeed better in case you don't call this code often.

 

Your code will go over all keys to find the related values.

 

If you would also maintain a SimpleSortingVector, every time your code will be called, you would usually iterate over less elements ( O(logn) steps to find the lowest date and then just the relevant elements).

 

Hope that helps,

 

E.

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: Iterate through hashtable of dates

Thank you maadani.  Do you have a sample illustrating this or somewhere online I can see this?  Thanks.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Iterate through hashtable of dates

How many values are you going to have in your hashtable?

Please use plain text.
Contributor
carll70
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: Iterate through hashtable of dates

My hashtable will holf around 80 or so key/value pairs. Won't be more than that.
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Iterate through hashtable of dates

For me, that is not a huge number, and so probably does not justify complications. 

 

I would design your data structure to optimize the most active usage and then do everything else by brute force. 

 

So if your usage is going to be mostly extraction of values between two dates, then use a sorted Vector, sort it manually if you think that is easier, and do a sequential or even binary lookup to find the values associated with a specific date.  If you are always looking up for dates, then stick with the hashtable, and then enumerate over it to total the value for a range.

Please use plain text.