Discussion:
hashmap in java7
Nimret Sandhu
2013-11-19 19:08:07 UTC
Permalink
Hi Dhilipan,
Did anyone notice some difference in hashmap in java 7 ?
what differences have you noticed?
btw I've created a new thread with your question.

cheers,
-
Nimret Sandhu
http://www.nimret.org
Douglas Pearson
2013-11-19 21:21:32 UTC
Permalink
We recently made the switch from Java 6 to Java 7 and didn't notice
anything changing in HashMap or really anywhere else for that matter.

We did notice overall that Java 7 was much faster (about 50% improvement)
for our app, although I suspect a lot of that may be better
threading/concurrency support.

Doug
Post by Nimret Sandhu
Hi Dhilipan,
Did anyone notice some difference in hashmap in java 7 ?
what differences have you noticed?
btw I've created a new thread with your question.
cheers,
-
Nimret Sandhu
http://www.nimret.org
Dhilipan Manoharan
2013-11-19 22:57:24 UTC
Permalink
Hi all,
 
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.


________________________________
From: Douglas Pearson <doug-list-S/***@public.gmane.org>
To: seajug-***@public.gmane.org
Sent: Tuesday, 19 November 2013 1:21 PM
Subject: Re: [seajug] hashmap in java7



 
We recently made the switch from Java 6 to Java 7 and didn't notice anything changing in HashMap or really anywhere else for that matter.

We did notice overall that Java 7 was much faster (about 50% improvement) for our app, although I suspect a lot of that may be better threading/concurrency support.

Doug



On Tue, Nov 19, 2013 at 11:08 AM, Nimret Sandhu <nimret-rf+Eeaps6PzQT0dZR+***@public.gmane.org> wrote:

 
Post by Nimret Sandhu
Hi Dhilipan,
 > Did anyone notice some difference in hashmap in java 7 ? 
what differences have you noticed?
btw I've created a new thread with your question.
cheers,
-Nimret Sandhu
http://www.nimret.org/
Eric Jain
2013-11-19 23:02:52 UTC
Permalink
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?


------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/seajug/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/seajug/join
(Yahoo! ID required)

<*> To change settings via email:
seajug-digest-***@public.gmane.org
seajug-fullfeatured-***@public.gmane.org

<*> To unsubscribe from this group, send an email to:
seajug-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo Groups is subject to:
http://info.yahoo.com/legal/us/yahoo/utos/terms/
Dhilipan Manoharan
2013-11-19 23:57:52 UTC
Permalink
I dont have any unit test case,but am running my application in two different machines having java6 and java7 and i noticed this behaviour.
 
Thanks,
Dhilipan M


________________________________
From: Eric Jain <eric.jain-***@public.gmane.org>
To: seajug <seajug-***@public.gmane.org>
Sent: Tuesday, 19 November 2013 3:02 PM
Subject: Re: [seajug] hashmap in java7



 
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
Dhilipan Manoharan
2013-11-20 18:04:06 UTC
Permalink
Here is what i found when i digged further.
 
after cloning a hashmap i am not able to find the key using containsKey method in j7.
 
I removed the clone and it finds the key.
 
Thanks,
Dhilipan M


________________________________
From: Dhilipan Manoharan <mdhilipan-/***@public.gmane.org>
To: "seajug-***@public.gmane.org" <seajug-***@public.gmane.org>
Sent: Tuesday, 19 November 2013 3:57 PM
Subject: Re: [seajug] hashmap in java7



 
I dont have any unit test case,but am running my application in two different machines having java6 and java7 and i noticed this behaviour.
 
Thanks,
Dhilipan M


________________________________
From: Eric Jain <eric.jain-***@public.gmane.org>
To: seajug <seajug-***@public.gmane.org>
Sent: Tuesday, 19 November 2013 3:02 PM
Subject: Re: [seajug] hashmap in java7



 
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
Douglas Pearson
2013-11-20 18:58:40 UTC
Permalink
Dhilipan this code works for me on Java 7. Does it fail for you?

If it works, can you modify it to show the problem you are experiencing?

@Test
public void testJava7Map() {
HashMap<String, Boolean> myMap = new HashMap<>() ;
myMap.put("Test", Boolean.TRUE) ;
assertTrue(myMap.get("Test") != null);

HashMap<String, Boolean> clonedMap = (HashMap<String,
Boolean>)myMap.clone() ;
assertTrue(clonedMap.size() > 0);
assertTrue(clonedMap.get("Test") != null);
assertTrue(clonedMap.containsKey("Test"));
}
Post by Dhilipan Manoharan
Here is what i found when i digged further.
after cloning a hashmap i am not able to find the key using containsKey method in j7.
I removed the clone and it finds the key.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:57 PM
*Subject:* Re: [seajug] hashmap in java7
I dont have any unit test case,but am running my application in two
different machines having java6 and java7 and i noticed this behaviour.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:02 PM
*Subject:* Re: [seajug] hashmap in java7
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string
key and it returns true when its running in java6 for a string key eligibly
present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
Nimret Sandhu
2013-11-20 18:59:10 UTC
Permalink
works as advertised :)

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()

per the docs clone() does a shallow copy - it does not copy over the
keys/values.

see http://goo.gl/zaGPmM for a deep copy/clone

cheers,
-
Nimret Sandhu
http://www.nimret.org
Post by Dhilipan Manoharan
Here is what i found when i digged further.
after cloning a hashmap i am not able to find the key using containsKey method in j7.
I removed the clone and it finds the key.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:57 PM
*Subject:* Re: [seajug] hashmap in java7
I dont have any unit test case,but am running my application in two
different machines having java6 and java7 and i noticed this behaviour.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:02 PM
*Subject:* Re: [seajug] hashmap in java7
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string
key and it returns true when its running in java6 for a string key eligibly
present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
Konstantin Ignatyev
2013-11-20 19:13:24 UTC
Permalink
What do you mean Nimret? Not cloning keys and values does _not_ mean they
are not present in the cloned map, only that they are the same objects as
in original map.

So keyExists should succeed on the cloned map provided that key has
consistent hash and properly implements hashCode and equals methods.
Post by Nimret Sandhu
works as advertised :)
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()
per the docs clone() does a shallow copy - it does not copy over the
keys/values.
see http://goo.gl/zaGPmM for a deep copy/clone
cheers,
-
Nimret Sandhu
http://www.nimret.org
Post by Dhilipan Manoharan
Here is what i found when i digged further.
after cloning a hashmap i am not able to find the key using containsKey method in j7.
I removed the clone and it finds the key.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:57 PM
*Subject:* Re: [seajug] hashmap in java7
I dont have any unit test case,but am running my application in two
different machines having java6 and java7 and i noticed this behaviour.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:02 PM
*Subject:* Re: [seajug] hashmap in java7
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string
key and it returns true when its running in java6 for a string key eligibly
present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
--
Konstantin Ignatyev

PS: If this is a typical day on planet Earth, humans will add fifteen
million tons of carbon to the atmosphere, destroy 115 square miles of
tropical rainforest, create seventy-two miles of desert, eliminate between
forty to one hundred species, erode seventy-one million tons of topsoil,
add 2,700 tons of CFCs to the stratosphere, and increase their population
by 263,000

Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a
Strategy for Reforming Universities and Public Schools. New York: State
University of New York Press, 1997: (4) (5) (p.206)
Nimret Sandhu
2013-11-20 22:10:03 UTC
Permalink
whups .. throw new RuntimeApiMisunderstoodException() .. thx for the
correction :)

cheers,
-
Nimret Sandhu
http://www.nimret.org


On Wed, Nov 20, 2013 at 11:13 AM, Konstantin Ignatyev
Post by Konstantin Ignatyev
What do you mean Nimret? Not cloning keys and values does _not_ mean they
are not present in the cloned map, only that they are the same objects as
in original map.
So keyExists should succeed on the cloned map provided that key has
consistent hash and properly implements hashCode and equals methods.
Post by Nimret Sandhu
works as advertised :)
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()
per the docs clone() does a shallow copy - it does not copy over the
keys/values.
see http://goo.gl/zaGPmM for a deep copy/clone
cheers,
-
Nimret Sandhu
http://www.nimret.org
Post by Dhilipan Manoharan
Here is what i found when i digged further.
after cloning a hashmap i am not able to find the key using containsKey method in j7.
I removed the clone and it finds the key.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:57 PM
*Subject:* Re: [seajug] hashmap in java7
I dont have any unit test case,but am running my application in two
different machines having java6 and java7 and i noticed this behaviour.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:02 PM
*Subject:* Re: [seajug] hashmap in java7
On Tue, Nov 19, 2013 at 2:57 PM, Dhilipan Manoharan <
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a
string key and it returns true when its running in java6 for a string key
eligibly present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
--
Konstantin Ignatyev
PS: If this is a typical day on planet Earth, humans will add fifteen
million tons of carbon to the atmosphere, destroy 115 square miles of
tropical rainforest, create seventy-two miles of desert, eliminate between
forty to one hundred species, erode seventy-one million tons of topsoil,
add 2,700 tons of CFCs to the stratosphere, and increase their population
by 263,000
Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a
Strategy for Reforming Universities and Public Schools. New York: State
University of New York Press, 1997: (4) (5) (p.206)
Dhilipan Manoharan
2013-11-21 03:09:45 UTC
Permalink
hi all,
 
To be more precise, containsKey fails for these values consistenly on a cloned map (5,10) but not for others.
 
Please guide me.
 
Thanks,
Dhilipan M


________________________________
From: Nimret Sandhu <nimret-rf+Eeaps6PzQT0dZR+***@public.gmane.org>
To: "seajug-***@public.gmane.org" <seajug-***@public.gmane.org>
Sent: Wednesday, 20 November 2013 2:10 PM
Subject: Re: [seajug] hashmap in java7



 
whups .. throw new RuntimeApiMisunderstoodException() .. thx for the correction :)


cheers,
-Nimret Sandhu
http://www.nimret.org/
What do you mean Nimret? Not cloning keys and values does _not_ mean they are not present in the cloned map, only that they are the same objects as in original map.
So keyExists should succeed on the cloned map provided that key has consistent hash and properly implements hashCode and equals methods.
 
Post by Nimret Sandhu
works as advertised :)
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()
per the docs clone() does a shallow copy - it does not copy over the keys/values.
see http://goo.gl/zaGPmM for a deep copy/clone
cheers,
-Nimret Sandhu
http://www.nimret.org/
Post by Dhilipan Manoharan
Here is what i found when i digged further.
 
after cloning a hashmap i am not able to find the key using containsKey method in j7.
 
I removed the clone and it finds the key.
 
Thanks,
Dhilipan M
Sent: Tuesday, 19 November 2013 3:57 PM
Subject: Re: [seajug] hashmap in java7
 
I dont have any unit test case,but am running my application in two different machines having java6 and java7 and i noticed this behaviour.
 
Thanks,
Dhilipan M
Sent: Tuesday, 19 November 2013 3:02 PM
Subject: Re: [seajug] hashmap in java7
 
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
--
Konstantin Ignatyev
 
PS: If this is a typical day on planet Earth, humans will add fifteen million tons of carbon to the atmosphere, destroy 115 square miles of tropical rainforest, create seventy-two miles of desert, eliminate between forty to one hundred species, erode seventy-one million tons of topsoil, add 2,700 tons of CFCs to the stratosphere, and increase their population by 263,000
 
Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a Strategy for Reforming Universities and Public Schools. New York: State University of New York Press, 1997: (4) (5) (p.206)
Douglas Pearson
2013-11-21 04:31:18 UTC
Permalink
Dhilipan - can you post some code that demonstrates what exactly you're
seeing?

A bit like my sample code earlier in the thread.

It'll help us a lot in figuring out what is going on.

Doug
Post by Dhilipan Manoharan
hi all,
To be more precise, containsKey fails for these values consistenly on a
cloned map (5,10) but not for others.
Please guide me.
Thanks,
Dhilipan M
*Sent:* Wednesday, 20 November 2013 2:10 PM
*Subject:* Re: [seajug] hashmap in java7
whups .. throw new RuntimeApiMisunderstoodException() .. thx for the correction :)
cheers,
-
Nimret Sandhu
http://www.nimret.org/
On Wed, Nov 20, 2013 at 11:13 AM, Konstantin Ignatyev <
What do you mean Nimret? Not cloning keys and values does _not_ mean they
are not present in the cloned map, only that they are the same objects as
in original map.
So keyExists should succeed on the cloned map provided that key has
consistent hash and properly implements hashCode and equals methods.
works as advertised :)
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()
per the docs clone() does a shallow copy - it does not copy over the keys/values.
see http://goo.gl/zaGPmM for a deep copy/clone
cheers,
-
Nimret Sandhu
http://www.nimret.org/
Here is what i found when i digged further.
after cloning a hashmap i am not able to find the key using containsKey method in j7.
I removed the clone and it finds the key.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:57 PM
*Subject:* Re: [seajug] hashmap in java7
I dont have any unit test case,but am running my application in two
different machines having java6 and java7 and i noticed this behaviour.
Thanks,
Dhilipan M
*Sent:* Tuesday, 19 November 2013 3:02 PM
*Subject:* Re: [seajug] hashmap in java7
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string
key and it returns true when its running in java6 for a string key eligibly
present in the map.
Do you have a unit test that demonstrates this behavior?
--
Eric Jain
zenobase.com -- What do you want to track today?
--
Konstantin Ignatyev
PS: If this is a typical day on planet Earth, humans will add fifteen
million tons of carbon to the atmosphere, destroy 115 square miles of
tropical rainforest, create seventy-two miles of desert, eliminate between
forty to one hundred species, erode seventy-one million tons of topsoil,
add 2,700 tons of CFCs to the stratosphere, and increase their population
by 263,000
Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a
Strategy for Reforming Universities and Public Schools. New York: State
University of New York Press, 1997: (4) (5) (p.206)
k***@u.washington.edu
2013-11-21 04:58:50 UTC
Permalink
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
What implementation of the JVM are you using?


Presumably the string pooling in the JVM implementation is the same,
but that's one thing to check.


Perhaps the trouble is the performance bug in JDK7 due to the new hashing

algorithm?


You didn't use -Djdk.map.althashing.threshold
and -XX:UseGetTimeOfDay,
correct?


Looking at the openjdk 7 source code, you can see the instance variable 'useAltHashing' in HashMap.


useAltHashing = sun.misc.VM.isBooted() &&

(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);


-Nichole




---In ***@yahoogroups.com, <doug-***@...> wrote:

Dhilipan - can you post some code that demonstrates what exactly you're seeing?

A bit like my sample code earlier in the thread.


It'll help us a lot in figuring out what is going on.


Doug

On Wed, Nov 20, 2013 at 7:09 PM, Dhilipan Manoharan <***@... mailto:***@...> wrote:
hi all,

To be more precise, containsKey fails for these values consistenly on a cloned map (5,10) but not for others.

Please guide me.

Thanks,
Dhilipan M



From: Nimret Sandhu <***@... mailto:***@...>
To: "***@yahoogroups.com mailto:***@yahoogroups.com" <***@yahoogroups.com mailto:***@yahoogroups.com>
Sent: Wednesday, 20 November 2013 2:10 PM
Subject: Re: [seajug] hashmap in java7



whups .. throw new RuntimeApiMisunderstoodException() .. thx for the correction :)


cheers,
-
Nimret Sandhu
http://www.nimret.org/ http://www.nimret.org/



On Wed, Nov 20, 2013 at 11:13 AM, Konstantin Ignatyev <***@... mailto:***@...> wrote:



What do you mean Nimret? Not cloning keys and values does _not_ mean they are not present in the cloned map, only that they are the same objects as in original map.

So keyExists should succeed on the cloned map provided that key has consistent hash and properly implements hashCode and equals methods.




On Wed, Nov 20, 2013 at 10:59 AM, Nimret Sandhu <***@... mailto:***@...> wrote:

works as advertised :)

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone() http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#clone()



per the docs clone() does a shallow copy - it does not copy over the keys/values.



see http://goo.gl/zaGPmM http://goo.gl/zaGPmM for a deep copy/clone



cheers,
-
Nimret Sandhu
http://www.nimret.org/ http://www.nimret.org/




On Wed, Nov 20, 2013 at 10:04 AM, Dhilipan Manoharan <***@... mailto:***@...> wrote:



Here is what i found when i digged further.

after cloning a hashmap i am not able to find the key using containsKey method in j7.

I removed the clone and it finds the key.

Thanks,
Dhilipan M



From: Dhilipan Manoharan <***@... mailto:***@...>
To: "***@yahoogroups.com mailto:***@yahoogroups.com" <***@yahoogroups.com mailto:***@yahoogroups.com>
Sent: Tuesday, 19 November 2013 3:57 PM
Subject: Re: [seajug] hashmap in java7



I dont have any unit test case,but am running my application in two different machines having java6 and java7 and i noticed this behaviour.

Thanks,
Dhilipan M



From: Eric Jain <***@... mailto:***@...>
To: seajug <***@yahoogroups.com mailto:***@yahoogroups.com>
Sent: Tuesday, 19 November 2013 3:02 PM
Subject: Re: [seajug] hashmap in java7
Post by Dhilipan Manoharan
The containsKey method when running in java7 returns false for a string key and it returns true when its running in java6 for a string key eligibly present in the map.
Do you have a unit test that demonstrates this behavior?

--
Eric Jain
zenobase.com http://zenobase.com/ -- What do you want to track today?






















































--
Konstantin Ignatyev

PS: If this is a typical day on planet Earth, humans will add fifteen million tons of carbon to the atmosphere, destroy 115 square miles of tropical rainforest, create seventy-two miles of desert, eliminate between forty to one hundred species, erode seventy-one million tons of topsoil, add 2,700 tons of CFCs to the stratosphere, and increase their population by 263,000

Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a Strategy for Reforming Universities and Public Schools. New York: State University of New York Press, 1997: (4) (5) (p.206)
Laird Nelson
2013-11-21 04:54:59 UTC
Permalink
To be more precise, containsKey fails for these values consistenly [sic]
on a cloned map (5,10) but not for others.
OK; finally bit on this one. No; no it doesn't.

Here's a main() method (stick it in whatever class you want) that works
just fine under Java 7, including autoboxing oddities:

public static final void main(final String[] args) throws Exception {

/*
* Here is proof that HashMap works just fine with Integers and no
* autoboxing.
*/

HashMap<Integer, Integer> original = new HashMap<Integer, Integer>();

Object old = original.put(Integer.valueOf(5), Integer.valueOf(10));
if (old != null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}

Object value = original.get(Integer.valueOf(5));
if (value == null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(Integer.valueOf(10))) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}

@SuppressWarnings("unchecked")
HashMap<Integer, Integer> clone = (HashMap<Integer,
Integer>)original.clone();
assert clone != null;

value = clone.get(Integer.valueOf(5));
if (value == null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(Integer.valueOf(10))) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}


/*
* Now let's try it with autoboxing.
*/
clone.clear();
assert clone.isEmpty();

original.clear();
assert clone.isEmpty();

old = original.put(5, 10);
if (old != null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}

value = original.get(5);
if (value == null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(Integer.valueOf(10))) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(10)) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}


/*
* OK, that works. Now cloning with autoboxing. We'll incur a
* compiler warning here.
*/

clone = (HashMap<Integer, Integer>)original.clone();
assert clone != null;

value = clone.get(5);
if (value == null) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(Integer.valueOf(10))) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
} else if (!value.equals(10)) {
throw new IllegalStateException("Josh Bloch and company have no idea
what they're doing");
}

}

I'm going to guess you have a threading issue of some kind? Please
remember that clone() is not by itself threadsafe. But that's just a guess.

Other things that have burned us grizzled veterans:
BigDecimal/BigInteger--watch your scale and precision, as equals() behaves
differently in these cases.

Best,
Laird
--
http://about.me/lairdnelson
Stuart Maclean
2013-11-20 19:35:34 UTC
Permalink
In the old days, the bucket count for a map was advised to be prime,
e.g. C = 101, so that after the hash() method was called on the object
to be either inserted or lookup, you would get a hopefully 'uniform'
distribution of bucketIDs, since the hash value mod C and bucket count C
would be co-prime. 101 was chosen i think because if you had to
increase capacity C, then C = 2C + 1 would still be prime for C = 203, 407.

Then, in Java 7 I think, it was realised that the expensive operation is
the mod operation needed in identifying the bucket. It would be MUCH
cheaper if C was a power of 2, since it reduces this

int bucket = hash % C

to this

int bucket = hash & (C-1)

which is is much more 'machine-oriented'. I can vouch for this since
one search problem I had was reduced from 2.5 hours to 1.5 hours solely
by replacing a 'mod' with an 'and', where the operation was called
millions of times in an inner loop.

Of course this may not be relevant to the current discussion but I
thought it worth sharing...

Stuart


------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/seajug/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/seajug/join
(Yahoo! ID required)

<*> To change settings via email:
seajug-digest-***@public.gmane.org
seajug-fullfeatured-***@public.gmane.org

<*> To unsubscribe from this group, send an email to:
seajug-unsubscribe-***@public.gmane.org

<*> Your use of Yahoo Groups is subject to:
http://info.yahoo.com/legal/us/yahoo/utos/terms/
Continue reading on narkive:
Loading...