aspose file tools*
The moose likes Java in General and the fly likes which Collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "which Collection?" Watch "which Collection?" New topic
Author

which Collection?

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I'm hoping to use a Collection for the following specs:
my requirement is that I have a class (MasterRate) with country code, calling rate, effective start date, effective end date ( there are a bunch more, but are just informational values ). I need to be able to hold the MasterRates in a collection, and search the collection which will bring back a match on the country code and DATE where DATE is between the effective start and effective end date.
I was thinking of modifying the MasterRate.equals() method to also check for equality with a new class ( we'll call it SearchRate ) which would have the same hashCode() method. Add the MasterRate's to a HashSet and let the HashSet do the work. But I'm a little uneasy about messing with the equals() method, and introducing some possible hard to trace bugs.
Any other suggestions or comments welcome
Jamie
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Hum IMHO you can't get there that way. HashMap relies heavily on the hashCode method for determining equality. To get SearchRate to return the same hashcode as MasterRate when appropriate would be difficult if not out and out impossible.
Hum, you will probably need several classes to assist in this. One which comes to mind is the MultiMap from Jakarta Commons Collections. Put them into a MultiMap using CountryCode as the key. MultiMap stores these in a collection each time you put a value with a key that exists, MultiMap adds the new object to the collection. Now, Have a Comarator class that compares two MasterRate Objects and sorts them by effectiveDate.
Now the process is:
1) Get the MasterRate List off for the CountryCode
2)Sort it by effectiveDate using Collections.sort.
3)Iterated through the list until you hit the correct one.
Not completely elegant but, should help minimize your processing.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Matthew Son
Greenhorn

Joined: Aug 20, 2003
Posts: 5
I may not understand your situation exactly, but I think you are going down the wrong path.
The Collection and Map interfaces are used for different purposes. The List and Set interfaces inherit from Collection and are used for ordered items and non-duplicate items respectively. Some concrete implementations of List are LinkedList and ArrayList. Some concrete implementations of Set are HashSet and TreeSet.
The Map interface associates items with key values (which is what you want). Some concrete implementations of Map are HashMap and TreeMap. I think you may want to use TreeMap. Here is the API: http://java.sun.com/products/jdk/1.2/docs/api/java/util/TreeMap.html
[ August 20, 2003: Message edited by: Matthew Son ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I was just going to post something similar. I think I've decided to try using a list( for simplicity ) sorted on country code, then iterating through the matched country codes to manually compare whether the DATE I have is between the effective dates.
thanks Carl
Jamie
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's a neat problem. I'd think about a concatenated string "key" of country code plus effective date and then make a tree of keys. If you can't find an exact match on keys, take one with the same country code and lower date. I'd also give some thought to nested structres. A hashmap keyed by country code might retrieve a tree or another hashmap keyed by dates.
Let us know what you wind up with!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Matthew Son
Greenhorn

Joined: Aug 20, 2003
Posts: 5
TreeMap is exactly what you are looking for.
TreeMap is an ordered mapping of objects with key values.
Make the key the date and use a combination of firstKey() and/or lastKey() to get the object you want.
I'm sure that trying to do this functionality through a list will be more difficult than learning the TreeMap API.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

not as straight forward as I hoped. I think I'll have a look at the multi-map or implement something similar. I can probably use a hashmap keyed on the country code, with a List of MasterRates as a value. If the key doesn't exist, add the MasterRate to a List and add the list to the HashMap. If the key does exist, retrieve the list for that country and add it to the list. I'll let you know what works.
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I could not find sufficient documentation for the jakarta MultiMap implementation, so I was hesitant to use it. So I threw together my own NestedMap ( there are a few convenience methods that have not been implemented due to time constraints like the addAll() method ):

let me know if you see any problems, it seems to work as expected
Jamie
[edited to fix values() method]
[ August 21, 2003: Message edited by: Jamie Robertson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which Collection?