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 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 Murach's Java Servlets and JSP this week in the Servlets 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: 19649

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
Similar Threads
immutable objects....
equals - hashcode
String object ?
hashcode() and equals()
How to System.out.println the string reference