Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap doubt . Thanks for comments.

 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • 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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(If you've already passed SCJP 5.0, is this really an SCJP question?)
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • 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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 557
  • Mark post as helpful
  • send pies
  • 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: 557
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • 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: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i can.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic