1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package org.example; import java.util.*; public class MyHashMap<K,V> { static final int DEFAULT_INITIAL_CAPACITY = 16; static final float DEFAULT_LOAD_FACTOR = 0.75f; int size = 0; Node<K,V>[] table = new Node[DEFAULT_INITIAL_CAPACITY]; public V put(K key, V value){ int hash = hash(key); int index = indexFor(hash, table.length); Node<K,V> head = table[index]; if (head == null){ table[index] = new Node<>(hash, key, value); size++; resizeIfNecessary(); return null; } while(true){ if (head.hash == hash && Objects.equals(head.key, key)){ V oldValue = head.value; head.value = value; return oldValue; } if (head.next == null) { head.next = new Node<>(hash, key, value); size++; resizeIfNecessary(); return null; } head = head.next; } } public V get(K key){ int hash = hash(key); int index = indexFor(hash, table.length); Node<K,V> head = table[index]; while (head != null){ if (head.hash == hash && Objects.equals(head.key, key)){ return head.value; } head = head.next; } return null; } public V remove(K key){ int hash = hash(key); int index = indexFor(hash, table.length); Node<K,V> head = table[index]; if (head == null) return null; if (head.hash == hash && Objects.equals(head.key, key)){ table[index] = head.next; size--; return head.value; } Node<K,V> pre = head; Node<K,V> cur = head.next; while (cur != null){ if (cur.hash == hash && Objects.equals(cur.key, key)){ pre.next = cur.next; size--; return cur.value; } pre = pre.next; cur = cur.next; } return null; } public boolean containsKey(K key){ int hash = hash(key); int index = indexFor(hash, table.length); Node<K,V> head = table[index]; while (head != null){ if (head.hash == hash && Objects.equals(head.key, key)){ return true; } head = head.next; } return false; } public int size(){ return size; }
public int hash(K key){ if (key == null) return 0; int h = key.hashCode(); return h ^ (h >>> 16); }
private int indexFor(int hash, int length) { return hash & (length - 1); } private void resizeIfNecessary(){ if (size <= table.length * DEFAULT_LOAD_FACTOR){ return; } Node<K,V>[] newTable = new Node[table.length * 2]; for (Node<K, V> head : this.table) { if (head == null) continue; Node<K,V> cur = head; while (cur != null){ Node<K,V> next = cur.next; int newIndex = indexFor(cur.hash, newTable.length); cur.next = newTable[newIndex]; newTable[newIndex] = cur; cur = next; } } this.table = newTable; System.out.println("扩容成功:" + table.length); }
static class Node<K,V>{ final int hash; final K key; V value; Node<K,V> next; public Node(int hash, K key, V value){ this.hash = hash; this.key = key; this.value = value; } } }
|