当通过Map.put(K,V)添加一个值时,是否必须通过Map.get(K)返回相同的实例? [英] When a value is added via Map.put(K, V), must the same instance be returned via Map.get(K)?
问题描述
假设您有以下代码:
地图< Foo, map = new HashMap< Foo,Bar>();
Foo foo = new Foo();
Bar bar = new Bar();
map.put(foo,bar);
Bar barReturned = map.get(foo);
Java是否需要 barReturned == bar
?也就是说,Java 需要 barReturned
与 bar $>相同的实例 c $ c>?如果没有,需要什么语义?
Javadoc 建议 barReturned == bar
必须为true,但我不能100% p>
V get(Object key)
返回指定键映射的值,如果此映射不包含键的映射,则返回
null
。
更正式地说,如果此映射包含从
k
到值v
,使得
(key == null?k == null:key.equals(k))
,则此方法返回v
;否则返回null
。 (最多只能有一个这样的映射。)
如果此映射允许
null
值, of null不一定 表示该映射不包含该键的映射;还有可能映射将键明确映射到null
。 containsKey操作可用于区分这两种情况。
参数:
key - 要返回其关联值的键
/ p>
或
null
如果此地图不包含键的映射
(强调我)
编辑:我理解,与标准库捆绑的 Map
的实现遵循 barReturned == bar
语义。我想知道的是这个行为是否必需根据文档。例如,如果我编写自己的实现 Map
?
如果你问是否可以打破这种关系,我认为答案是是。例如,如果您正在实现一个类似持久高速缓存的映射,那么特定值可能会写入磁盘,如果在一段时间内未使用,则稍后重新加载。你在这种情况下不会有参考平等,但是没关系。显然,你想要记录与标准行为的任何偏差,但我不认为这是一个合理使用地图的范围。
Suppose you have this code:
Map<Foo, Bar> map = new HashMap<Foo, Bar>();
Foo foo = new Foo();
Bar bar = new Bar();
map.put(foo, bar);
Bar barReturned = map.get(foo);
Does Java require that barReturned == bar
? That is, does Java require that barReturned
be the same instance as bar
? If not, what semantics are expected?
The Javadoc suggests that barReturned == bar
must be true, but I'm not 100% sure:
V get(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key
k
to a valuev
such that(key==null ? k==null : key.equals(k))
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)If this map permits
null
values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull
. The containsKey operation may be used to distinguish these two cases.Parameters:
key
- the key whose associated value is to be returnedReturns:
THE VALUE TO WHICH THE SPECIFIED KEY IS MAPPED, or
null
if this map contains no mapping for the key
(Emphasis mine)
Edit: I understand that the implementations of Map
that come bundled with the standard library adhere to the barReturned == bar
semantics. What I want to know is whether or not this behavior is required as per the documentation. For example, must I also adhere to these semantics if I write my own class that implements Map
?
If you are asking whether you can break that relation, i think the answer is "yes". for instance, if you were implementing a Map which acted like a persistent cache, a specific value may be written to disk if not used in a while, then reloaded later. you will not have reference equality in that situation, but that's okay. obviously, you would want to document any deviations from standard behavior, but i don't think this is out of the realm of a reasonable usage of a Map.
这篇关于当通过Map.put(K,V)添加一个值时,是否必须通过Map.get(K)返回相同的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!