This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Other JSE/JEE APIs and the fly likes String hash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "String hash" Watch "String hash" New topic

String hash

Abhijit Jawale

Joined: Jun 01, 2004
Posts: 4
When String hash is computed?
If I am inserting 10 million strings in a HashSet then will it compute it for each insertion or it would have already computed hash (as String is immutable object)?
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
The hash will be computed for every object added in the set.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19674

Although officially it is unspecified, String has a transient field for the hash code. This field is calculated when hashCode is called and the field is 0. So unless the hash code of a String actually 0 it will be calculated only once.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: String hash