//===================hashmap begin================== public interface HBMap<K,V> { //map的put方法 V put(K k,V v); //map的get方法 V get(K k); //size方法 int size(); //map內部接口entry public interface Entry<K,V> { V getValue(); K getKey(); } } public class HBHashMap<K,V> implements HBMap<K, V>{ //數組初始默認長度 private static int defaultLength = 16; //設置擴用因子 private static double defaultLoader = 0.75; //數組容器 Entry<K,V>[] table = null; //數組長度 private int size = 0; //有參構造 個性化設置 public HBHashMap(int length,double loader) { defaultLength = length; defaultLoader = loader; table = new Entry[defaultLength]; } //無參構造 使用默認設置 public HBHashMap() { this(defaultLength,defaultLoader); } @Override public V put(K k, V v) { //若是超過警惕值,數組進行擴容 if (size > defaultLength * defaultLoader) { up2size(); } //一、獲取key計算後的下標 int index = getIndex(k); Entry<K,V> entry = table[index]; if (table[index] == null) { //若是當前下標的在數組裏沒有值,則直接插入 table[index] = newEntry(k,v,null); //數組長度+1 size++; } else { table[index] = newEntry(k,v,entry.next); } return table[index].getValue(); } private void up2size () { Entry<K,V>[] newTable = new Entry[defaultLength*2]; againHash(newTable); } private void againHash(Entry<K,V>[] newTable) { //存取擴容前全部entry對象 List<Entry<K,V>> list = new ArrayList<Entry<K,V>>(); for (int i=0;i<table.length;i++) { if (table[i] == null) { continue; } findEntryByNext(table[i],list); } if (list.size() > 0) { defaultLength = defaultLength * 2; size = 0; table = newTable; for(Entry<K,V> entry:list) { if (entry.next != null) { entry.next = null; } put(entry.getKey(),entry.getValue()); } } } private void findEntryByNext1 (Entry<K, V> entry , List<Entry<K, V>> list) { if (entry != null && entry.next != null) { list.add(entry); findEntryByNext(entry.next,list); } else { list.add(entry); } } private void findEntryByNext(Entry entry,List<Entry<K,V>> list) { if (entry.next == null) { list.add(entry); return; } else { list.add(entry); findEntryByNext(entry.next,list); } } private Entry<K,V> newEntry(K k,V v,Entry entry) { return new Entry(k,v,null); } private int getIndex(K k) { int m = getMaxPrime(); int index = k.hashCode()%m; return index>0? index:-index; } //取比數組長度小的最大質數 private int getMaxPrime() { for (int i=defaultLength;i>0;i--) { if (defaultLength%2 != 0) { return i; } } //若是沒找到返回數組當前長度 return defaultLength; } @Override public V get(K k) { int index = getIndex(k); return findValueByKey(k,table[index]); } private V findValueByKey (K k,Entry<K,V> entry){ if (k == entry.getKey() || k.equals(entry.getKey())) { return entry.getValue(); } else { if (entry.next != null) { return findValueByKey (k,entry.next); } } return entry.getValue(); } @Override public int size() { return 0; } //內部類實現entry接口 class Entry<K,V> implements HBMap.Entry<K, V> { V v; K k; Entry next; public Entry(K k ,V v,Entry next) { this.v = v; this.k = k; this.next = next; } @Override public V getValue() { return v; } @Override public K getKey() { return k; } } } //===================hashmap end==================== //===================list begin==================== public interface HBList<E> { //獲取集合長度 public int size(); //向集合中添加元素 public boolean add(E paramE); //移除數組元素 public boolean remove(Object paramObject); //獲取集合下表對應的元素 public E get(int paramInt); } public class HBArrayList<E> implements HBList<E>{ //elementData存放說有集合數據 transient不被序列化 transient Object[] elementData; //初始數組容量設置爲空 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0]; private int size; public HBArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } transient int modCount = 0; @Override public int size() { return this.elementData.length; } @Override public boolean add(E paramE) { ensureCapacityInternal(this.size()+1); this.elementData[(this.size++)] = paramE; return true; } private void ensureCapacityInternal(int index) { index = getCurrentIndex (index); ensureExplicitCapacity(index); } private void ensureExplicitCapacity(int index) { this.modCount+=1; if (index - elementData.length <= 0) return; grow(index); } private void grow(int index) { int oldElementLength = this.elementData.length; int newElementLength = oldElementLength + (oldElementLength>>1); //首次擴容的特殊處理 if (index - newElementLength > 0) newElementLength = index; //執行擴容操做 this.elementData = Arrays.copyOf(this.elementData, newElementLength); } private int getCurrentIndex (int index) { if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(10, index); } return index; } @Override public boolean remove(Object paramObject) { return false; } @Override public E get(int paramInt) { return (E) this.elementData[paramInt]; } } public class HBLinkedList<E> implements HBList<E> { transient int size; transient int modConut; transient Node<E> last; transient Node<E> first; @Override public int size() { return 0; } @Override public boolean add(E paramE) { Node<E> node = this.last; Node<E> newNode = new Node<E>(node,paramE,null); if (node == null) { this.first = newNode; } this.last = newNode; this.size+=1; this.modConut+=1; return true; } @Override public boolean remove(Object paramObject) { return false; } @Override public E get(int paramInt) { if (paramInt >=0 && paramInt < this.size) { Node<E> node = this.last; for (int i=paramInt;i<this.size-1;i++) node = node.prve; return node.item; } return (E) "是否是下標過大,請檢查.."; } } //===================list end======================