• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap problem

 
Shivkalyan Chavan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my application I put key, value pair using Hashmap. Later I use these values for sending mail. I am facing following question related problem..

1) Is the hashmap values are refreshed only when web server restarted?
2) Suppose I used index keys to retrieve values then evary time I use the same index value for that value or it get different value on random selection.
3) Is any alternative solution except HashTable to key and value pair..?

Please help to sort out these questions..

Regards,
Shivkalyan
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shivkalyan Chavan, welcome to Javaranch,
I will answer your questions but will you please tell me first what do they have to do with servlets?
Please take a glance through the Ask good question link in my signature below.

The moderators will move it to a proper, I'll post my reply there.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shivkalyan Chavan:
1) Is the hashmap values are refreshed only when web server restarted?
2) Suppose I used index keys to retrieve values then evary time I use the same index value for that value or it get different value on random selection.
3) Is any alternative solution except HashTable to key and value pair..?
  • 1: Don't know. Have you set up any refreshing code?
  • 2: Read the API for a Map. It is a "function": same input--->same output.
  • 3: Look up implementations of the Map interface. Most people use HashMap nowadays instead of Hashtable.

  • [ June 24, 2008: Message edited by: Campbell Ritchie ]
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20494
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Shivkalyan Chavan:
    2) Suppose I used index keys to retrieve values then evary time I use the same index value for that value or it get different value on random selection.

    You get the same value each time, unless you overwrite the old value by another put operation.
    That's exactly how Maps work - they are a mapping (hence the name) from a key to a value. You as the programmer determine what the mappings will be, by issuing put operations. When you then use a get operation it will retrieve the value you have stored last with a put operation.

    Keep this in mind though:
    When you are using a HashMap or Hashtable, the equals and hashCode methods will be used to do the lookups. The hashCode value can never change, or you will not be able to retrieve the value. That's why String and Integer are excellent for keys - once created these objects never change.

    When you are using TreeMap the same holds for the compareTo method.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic