摘自
1.定义接口
public interface MyMap < K , V > {
public V put ( K k, V v) ;
public V get ( K k) ;
interface Entry < K , V > {
public K getKey ( ) ;
public V getValue ( ) ;
}
}
2. 主要部分
import java. util. ArrayList ;
import java. util. List ;
public class MyHashMap < K , V > implements MyMap < K , V > {
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4 ;
private static final float DEFAULT_LOAD_FACTOR = 0.75f ;
private int defaultInitSize;
private float defaultLoadFactor;
private int entryUserSize;
private Entry < K , V > [ ] table = null ;
public MyHashMap ( ) {
this . defaultLoadFactor = DEFAULT_LOAD_FACTOR;
this . defaultInitSize = DEFAULT_INITIAL_CAPACITY;
table = new Entry [ this . defaultInitSize] ;
}
public MyHashMap ( int defaultInitSize, int defaultLoadFactor) {
this . defaultInitSize = defaultInitSize;
this . defaultLoadFactor = defaultLoadFactor;
table = new Entry [ this . defaultInitSize] ;
}
class Entry < K , V > implements MyMap. Entry < K , V > {
private K key;
private V value;
private Entry < K , V > next;
public Entry ( ) { }
public Entry ( K key, V value, Entry < K , V > next) {
this . key = key;
this . value = value;
this . next = next;
}
@Override
public K getKey ( ) {
return key;
}
@Override
public V getValue ( ) {
return value;
}
}
@Override
public V put ( K k, V v) {
V oldValue = null ;
if ( entryUserSize >= defaultInitSize * defaultLoadFactor) {
resize ( 2 * defaultInitSize) ;
}
int index = hash ( k) & ( defaultInitSize- 1 ) ;
if ( table[ index] == null ) {
table[ index] = new Entry < K , V > ( k, v, null ) ;
++ entryUserSize;
} else {
Entry < K , V > entry = table[ index] ;
Entry < K , V > e = entry;
while ( e != null ) {
if ( k == e. getKey ( ) || k. equals ( e. getKey ( ) ) ) {
oldValue = e. value;
e. value = v;
return oldValue;
}
e = e. next;
}
table[ index] = new Entry < K , V > ( k, v, entry) ;
++ entryUserSize;
}
return oldValue;
}
private int hash ( K k) {
int h;
return ( k == null ) ? 0 : Math . abs ( h = k. hashCode ( ) ) ^ ( h >>> 16 ) ;
}
private void resize ( int i) {
Entry [ ] newTable = new Entry [ i] ;
defaultInitSize = i;
entryUserSize = 0 ;
rehash ( newTable) ;
}
private void rehash ( Entry < K , V > [ ] newTable) {
List < Entry < K , V > > entryList = new ArrayList < > ( ) ;
for ( Entry < K , V > entry : table) {
if ( entry != null ) {
while ( entry != null ) {
entryList. add ( entry) ;
entry = entry. next;
}
}
}
if ( newTable. length > 0 ) {
table = newTable;
}
for ( Entry < K , V > entry : entryList) {
put ( entry. getKey ( ) , entry. getValue ( ) ) ;
}
}
@Override
public V get ( K k) {
int index = hash ( k) & ( defaultInitSize- 1 ) ;
if ( table[ index] == null ) {
return null ;
} else {
Entry < K , V > entry = table[ index] ;
while ( entry != null ) {
if ( k == entry. getKey ( ) || k. equals ( entry. getKey ( ) ) ) {
return entry. getValue ( ) ;
}
entry = entry. next;
}
}
return null ;
}
}
3. 测试部分
public class Test {
public static void main ( String [ ] args) {
MyMap < String , String > myMap = new MyHashMap < > ( ) ;
for ( int i = 0 ; i < 200 ; i++ ) {
myMap. put ( "key" + i, "value" + i) ;
}
for ( int i = 0 ; i < 200 ; i++ ) {
System . out. println ( "key" + i+ "value is : " + myMap. get ( "key" + i) ) ;
}
}
}