我想使用哈希表将不可变的字符串ID唯一地链接到可变对象.如果我有两个相同字符的字符串,Java会将它们解释为哈希表的不同键吗?我知道字符串是对象,所以两个字符串可能没有相同的引用.
换句话说,如果我这样做......
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
Run Code Online (Sandbox Code Playgroud)
我可以安全地使用字符串作为哈希表的键(意味着由相同字符组成的字符串将返回相同的值)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |