Results 1 to 4 of 4
  1. #1
    whatif is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Java hashtable & hashmap

    Hello,

    How is it possible that hashtable and hashmap don't allow for duplicates? Don't they implement separate chaining hashtable, where keys yielding the same hash get to be saved in the list at a specific index.

    Thanks in advance for clarifying!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Java hashtable & hashmap

    Identical keys generate identical hashcodes. Different keys may or may not generate the same hashcode. The hash map is essentially an array of linked lists. The hashcode is used to index into the proper linked list for the desired item. That linked list is then searched to find a value for the current key. If the list had two values for the same key, which one would it return? I guess it could have been written to return a series of values for a given key. But the current implementation will check to see if a value already exists for the supplied key. If it does, the old value is replaced with the new value.

    Regards,
    Jim
    Last edited by jim829; 01-14-2014 at 10:27 PM. Reason: grammar
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    whatif is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: Java hashtable & hashmap

    I was interested in a hashtable because of its performance. Do you suggest another data structure to store strings? A String will be the key and tt will get more than 1 million entry. Didn't want to use arrays because of the delay to reorder and delete and add.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Java hashtable & hashmap

    For large data collections a hashmap is certainly faster than the linear lookup of an array. The HashMap implementation will essentially double the table size once it gets to a certain threshhold. That means that the entire table is rehashed when the table is resized. So you may want to tune that parameter and the initial allocation. Of course, I don't know how you will use the table but depending on your data and access requirements you might consider a database.

    Regards,
    Jim
    Last edited by jim829; 01-15-2014 at 12:32 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM
  2. Problem using HashTable and HashMap duplicate insertions
    By omerkhalid34 in forum Advanced Java
    Replies: 1
    Last Post: 01-14-2012, 02:41 PM
  3. How does HashMap differ from HashTable?
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 08-12-2011, 04:05 PM
  4. HashTable vs HashMap
    By bugger in forum New To Java
    Replies: 7
    Last Post: 01-06-2011, 03:15 PM
  5. Replies: 7
    Last Post: 12-08-2009, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •