• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which Collection?

 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Matthew Son
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic