javaは簡単なHashMapを実現します.


1.HashMap初期化サイズHashEntry[]を定義します.複数のHash Entryからなる配列2.HashMapが拡張を行う閾値負荷因子閾値threshhold=(int)(DEFAULTU INITIAL LuCAPACITY*DEFAULTAT-uFACTOR)を定義します.3.hashMapにEntryを搭載したフォーマットsize Entry(key,value,next)put操作を定義します.まずkeyに対応するEntryを見つけて、Entryチェーンremove操作を巡回します.同じです.
package DataStruct;


/**
 * Description:HashMap  
* Copyright (c) , 2019, LafreeZhao
* This program is protected by copyright laws.
* Date:2019 03 05 * * @author * @version : 1.0 */
public class MyHashMap { // 16 private static final int DEFAULT_INITIAL_CAPACITY = 16; // 0.75 private static final float DEFAULT_LOAD_FACTOR = 0.75f; // private int threshold; // private int size; // private int resize; private HashEntry[] table; public MyHashMap() { table = new HashEntry[DEFAULT_INITIAL_CAPACITY]; threshold = (int) (DEFAULT_INITIAL_CAPACITY*DEFAULT_LOAD_FACTOR); size =0 ; } private int index(Object key){ // key hashcode table key table return key.hashCode()%table.length; } public void put(Object key,Object value){ //key null , if (key == null) { return; } int index = index(key); // index entry, key entry , HashEntry entry = table[index]; while(entry != null){ if (entry.getKey().hashCode() == key.hashCode() && entry.getKey() == key || entry.getKey().equals(key)){ entry.setValue(value); return; } entry = entry.getNext(); } // index entry key, key table index add(index,key,value); } private void add(int index, Object key, Object value) { // entry table index , HashEntry entry = new HashEntry(key,value,table[index]); table[index] = entry; // size , , table capacicy if (size++ >=threshold){ resize(table.length*2); } } public void resize(int capacity) { if (capacity <=table.length){ return; } HashEntry[] newTable = new HashEntry[capacity]; // table, entry hash newTable for (int i=0;i<table.length;i++){ HashEntry old = table[i]; while(old !=null){ HashEntry next = old.getNext(); int index = index(old.getKey()); old.setNext(newTable[index]); old = next; } } table = newTable; threshold = (int) (table.length*DEFAULT_LOAD_FACTOR); resize++; } public Object get (Object key){ if (key == null){ return null; } HashEntry entry = getEntry(key); return entry == null?null:entry.getValue(); } private HashEntry getEntry(Object key) { HashEntry entry = table[index(key)]; while(entry !=null){ if (entry.getKey().hashCode() == key.hashCode() ||entry.getKey() == key || entry.getKey().equals(key)){ return entry; } entry = entry.getNext(); } return null; } public void remove(Object key){ if (key == null){return;} int index=index(key); HashEntry pre = null; HashEntry entry = table[index]; while(entry !=null){ if (entry.getKey().hashCode() == key.hashCode() && (entry.getKey() == key || entry.getKey().equals(key))){ if (pre == null){ table[index]=entry.getNext(); }else pre.setNext(entry.getNext()); // haul size--; return; } pre = entry; entry = entry.getNext(); } } public boolean containsKey(Object key){ if (key == null){ return false; } return getEntry(key)!=null; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(String.format("size:%s capacity:%s resize:%s

"
, size, table.length, resize)); for (HashEntry entry : table) { while (entry != null) { sb.append(entry.getKey() + ":" + entry.getValue() + "
"
); entry = entry.getNext(); } } return sb.toString(); } } class HashEntry{ private Object key; private Object value; private HashEntry next; public HashEntry(Object key, Object value, HashEntry next) { this.key = key; this.value = value; this.next = next; } public Object getKey() { return key; } public void setKey(Object key) { this.key = key; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } public HashEntry getNext() { return next; } public void setNext(HashEntry next) { this.next = next; } } }