[英]Java HashMap and Object keys
LinkedHashMap<Long, String> myHashMap = new LinkedHashMap<>();
myHashMap.put(new Long(1), "A Value");
String aValue = myHashMap.get(new Long(1));
String aValue = myHashMap.get(new Long(1));
, will I get "A Value"
back? "A Value"
吗? Or have I just queried for a different object (reference) and therefore I'll get an error? ==
(except for IdentityHashMap). ==
进行比较(IdentityHashMap除外)。 You could test that pretty easily, BTW. You will get "A Value"
back, because Long
has overridden 您将获得
"A Value"
,因为Long
已被覆盖
equals()
( return value == obj.longValue()
), equals()
( return value == obj.longValue()
), hashCode()
( return Long.hashCode(value)
). hashCode()
( return Long.hashCode(value)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.