• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Properties class and HashTable,HashMap

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following key value pairs:
accountnumber 1
accounttype 2
status 3

These are stored in HashMap,HashTable.Also I use Properties class which loads these values from a property file.

Now I do the following within my code:

1)int i = ((Integer)hashTable.get("accountnumber")).intValue();

2)int j = ((Integer)hashMap.get("accountnumber")).intValue();

3)int k = ((Integer)properties.get("accountnumber")).intValue();

However I get a ClassCastException in the third case..Why?How does it differ from the previous 2 cases.

Also in the first 2 cases how can I cast the Object returned by get method to Integer?Doesn't that amount to narrow casting which should give ClassCasException??

Kindly clarify my doubts.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Properties only store values as strings. So, you can not cast it to Integer. You can use a Integer.parseInt() for getting int from the string value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic