This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashMap doubt . Thanks for comments. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap doubt . Thanks for comments." Watch "HashMap doubt . Thanks for comments." New topic
Author

HashMap doubt . Thanks for comments.

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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 ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

(If you've already passed SCJP 5.0, is this really an SCJP question?)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
If you have problems with rehashing you can always design your own implementation of the Map interface . . .
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
yes i can.
 
Consider Paul's rocket mass heater.
 
subject: HashMap doubt . Thanks for comments.