I want to use a hashtable to uniquely link immutable string IDs to mutable objects. If I have two strings of the same characters, will Java interpret them as different keys to the hash table? I know that Strings are objects, so two strings might not have the same reference.
In other words, if I do …
myHashTable.add("A" , ObjectA)
String myReference = "A"
myHashTable.add(myReference, ObjectB) // I am not sure how java will interpret this
myHashTable.get("A") //returns A
myHashTable.get(myReference) //returns B
myReference.equals("A") //returns true
Can I safely use strings as keys to the hashtable (meaning that strings made up of the same characters will return the same value)
The
String.hashCode()method is based only on the content of the string, not the object identity. So yes, this is safe.