HasMap
LinkedHashMap(LruCache) 缓存
散列链表
public interface Map<K,V>{
public static interface Entry<K,V>{
public boolean equals(Object object);
public K getKey();
public V getValue();
public int hashCode();
public V setValue(V object);
};
public void clear();
public boolean containsKey(Object key);
public boolean containValue(Object value);
public Set<Map.Entry<K,V>> entrySet();
public boolean equals(Object object);
public V get(Object key);
public int hasCode();
public boolean isEmpty();
public Set<K> keySet();
public V put(K key,V value);
public void putAll(Map<? extends K,? extends V> map);
public V remove(Object key);
public int size();
public Collection<V> values();
}
public abstract class AbstractMap<K,V> implements Map<K,V>{
Set<K> keySet;//懒加载 Set集合里面的值唯一
Collection<V> valuesCollection; //Collection 集合里面的值可以重复
public static class SimpleImmutableEntry<K,V> implements Map.Entry<K,V>,Serializable{
private static final long serialVersionUID=7138329143949025153L;
private final K key;
private final V value;
public SimpleImmutableEntry(K theKey,V theValue){
key=theKey;
value=theValue;
}
public SimpleImmutableEntry(Map.Entry<? extends K, ? extends V> copyFrom){
key=copyFrom.getKey();
value=copyFrom.getValue();
}
public K getKey(){
return key;
}
public V getValue(){
return value;
}
public V setValue(V object){
throw new UnsupportedOperationException();
}
public boolean equals(Object object){
if(this==object){
return true;
}
if(object instanceof Map.Entry){
Map.Entry<?,?> entry=(Map.Entry<?,?>)object;
return (key==null?entry.getKey()==null:key.equals(entry.getKey())) && (value==null?entry.getValue()==null:value.equals(entry.getValue()));
}
return false;
}
public int hashCode(){
return (key==null?0:key.hashCode())^(value==null?0:value.hashCode());
}
public String toString(){
return key+"="+value;
}
}
public static class SimpleEntry<K,V> implements Map.Entry<K,V>,Serializable{
private static final long serialVersionUID=-8499721149061103585L;
private final K key;
private V value;
public SimpleEntry(K theKey,V theValue){
key=theKey;
value=theValue;
}
}
}
public class HasMap<K,V> extends AbstractMap<K,V> implements Cloneable,Serializable{
transient HashMapEntry<K,V>[] table;//短暂的
public V put(K key,V value){
if(key==null){
return putValueForNullKey(value);
}
int hash=Collections.secondaryHash(key);
HashMapEntry<K,V>[] tab=table;
int index=hash & (tab.length-1);
for(HashMapEntry<K,V> e=tab[index]; e!=null; e=e.next){
if(e.hash==hash && key.equals(e.key)){
preModify(e);
V oldValue=e.value;
return oldValue;
}
}
}
}
public class Collections{
private static final Iterator<?> EMPTY_ITERATOR=new Iterator<Object>(){
public boolean hasNext(){
return false;
}
public Object next(){
throw new NoSuchElementException();
}
public void remove(){
throw new IllegalStateException();
}
....
public static int secondaryHash(Object key){
return secondaryHash(key.hashCode());
}
...
//spread bits to regularize both segment and index locations
//扩展位调整到段和索引位置
//using variant of single-word wang/jenkins hash
//运用wang/jekins 哈希的可变体单字算法
private static int secondaryHash(int h){
h+=(h<<15)^0xffffcd7d; // 4294954365 11111111111111111100110101111101
h^=(h>>>10);
h+=(h<<3);
h^=(h>>>6);
h+=(h<<2)+(h<<14);
return h^(h>>>16);
}
}
}
注意下面的方法:
private static int secondaryHash(int h){
h+=(h<<15)^0xffffcd7d; // 4294954365 11111111111111111100110101111101
h^=(h>>>10);
h+=(h<<3);
h^=(h>>>6);
h+=(h<<2)+(h<<14);
return h^(h>>>16);
}
这里注释写的很清楚用了 wang/jekins 哈希算法;