This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread 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 Elasticsearch in Action this week in the Big Data 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: 19905

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
wood burning stoves
subject: String hash