• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtables, objects & casting  RSS feed

 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello jolly-javaranchers!
My need is to get a Big Decimal value out of a Hashtable entry.
I was gonna ask how, but in the process of "trying it" I found two ways which both work as needed.

At this point, the only question I have is "Is either way preferable?"


Regards, Barb
[ August 23, 2006: Message edited by: Barb Rudnick ]
 
Cosima Laube
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barb,

if you can be sure to get only BigDecimals out of you HashTable then I woul prefer the second solution because there you only cast the Object references you get out of the Table back to what they really are: BigDecimals. The first solution would create totally new BigDecimal-Objects on the heap which is - in my point of view - not necessary.

Hope this helps!
Regards,
Cosima
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using JDK 1.5? If so, there are collections that are preferable to the use of Hashtable. In particular, there is the HashMap class.

Also 1.5 allows you to use generics. So when creating the HashMap you can specify that it is to hold BigDecimal objects, and when you retrieve an object from the HashMap, you don't have to worry about casting.
 
Barb Rudnick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you can be sure to get only BigDecimals out of you HashTable then I would prefer the second solution because there you only cast the Object references you get out of the Table back to what they really are: BigDecimals. The first solution would create totally new BigDecimal-Objects on the heap which is - in my point of view - not necessary.


Thanks Cosima! Ensuring receipt of only BigDecimals is what I was concerned about. I know no way of doing this EXCEPT to code the method that loads the Hashtable. Which is what I will do.



Are you using JDK 1.5? If so, there are collections that are preferable to the use of Hashtable. In particular, there is the HashMap class.

Also 1.5 allows you to use generics. So when creating the HashMap you can specify that it is to hold BigDecimal objects, and when you retrieve an object from the HashMap, you don't have to worry about casting.


Thanks Keith! Nope, JDK 1.4. Though I often dream of progress.....

Thanks again all!

Barb
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!