我有一个关于 .get(key) 如何工作的问题。
如果我使用复杂对象作为键,我发现不调用 equals 函数。但是其中引用了用于识别关键对象的框架。
我需要知道哪个更快?字符串或对象作为键?
这是 get 函数,getCacheControl()
正在返回一个IMemoryCache<K, V>
实现。
@Override
public V get( K name )
{
ICacheElement<K, V> element = this.getCacheControl().get( name );
return ( element != null ) ? element.getVal() : null;
}
AbstractMemoryCache 实现 IMemoryCache 并使用 Map,
https://github.com/apache/commons-jcs/blob/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/ AbstractMemoryCache.java
/** Map where items are stored by key. This is created by the concrete child class. */
public Map<K, MemoryElementDescriptor<K, V>>
所以我们可以假设类似的语义。复合对象不应该有任何区别,也许你忘了覆盖hashCode
. Map 首先使用它来查找存储桶,然后使用equals
.