• 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

HashMap doubt . Thanks for comments.

 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Can't we guarentee every Entry to be put in Map ?

Any ideas. Please post.
[ April 19, 2007: Message edited by: Srinivasan thoyyeti ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
...Can't we guarentee every Entry to be put in Map? ...


I'm not sure exactly what you're asking. Duplicate keys would obviously present a problem. The documentation for HashMap's put method says, "If the map previously contained a mapping for this key, the old value is replaced." (But even in this case, every entry is technically "put" into the Map, although some might be replacing other entries.)

Note that the put method returns the "previous value associated with specified key, or null if there was no mapping for key." So if a non-null value is being replaced, you could detect it this way.
[ April 17, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(If you've already passed SCJP 5.0, is this really an SCJP question?)
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi marc,

Coming to my question:
I am much bothered about the abondoned Entries which will fall outside bucket array length.

Positive:
If we are sure about the hashCode return values Like (expression)%10. [0-9]
Then we can have HashMap with 10 buckets
new HashMap(10,1); (10-buckets,1-loadFactor).
Here we are able to put all entries in MAP.

But one contradiction found here: Why rehash done like that?:
rehasing done
if No of Entries in Map >= threshhold (= no of buckets * loadfactor)
I don't understand whats the need to rehash(increase the no of buckets), as the hashCode returning 0-9 indexes.

Negetive:
Whats the criteria to select the no of buckets if we are not sure about the bucket number hashCode method will return SO as to accomodate each entry ?

Please respond...!
[ April 18, 2007: Message edited by: Srinivasan thoyyeti ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Negetive:
Whats the criteria to select the no of buckets if we are not sure about the bucket number hashCode method will return SO as to accomodate each entry ?



The hashcode is used to map to the correct bucket -- it is *not* the bucket number. The hashmap and hashset collections do not assume that the hashcode is within any value, it can take any legal value of an int.

Henry
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

I am delighted to see your bytes.

I am glad to find
final int hash(Object key)
{
return key == null ? 0 : Math.abs(key.hashCode() % buckets.length);
}

That method flushed all my doubts;
rehash method will cause buketlength increase when threshhold reaches so as to scatter the Entries. Lovely.

Thanks a lot.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Still i am left with one more doubt: (Please look-into this)

Is there any way to stop rehash not to occur in HashMap?

Why i don't want rehash in some cases:
Consider when hasCode(Key) returns (expression)%10.

Here user can select HashMap hm = new HashMap(10); [10-bucket size]
But the HashMap keep on increasing the Buket length as and when ThreshHold point reaches.

Solution i think:
Considering cases like hashCode(Key) --> (expression)%n.

Why can't we provide a separate HashMap constructor parameter which will be used to cancel rehashing up on user request.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I understand what problem you are trying to solve. What is the problem with the HashMap doing a rehash when it's getting full?
 
Marshal
Posts: 79401
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have problems with rehashing you can always design your own implementation of the Map interface . . .
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i can.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic