| 17 | LinkedList<MapEntry<K, V>>[] buckets = |
| 18 | new LinkedList[SIZE]; |
| 19 | @Override public V put(K key, V value) { |
| 20 | V oldValue = null; |
| 21 | int index = Math.abs(key.hashCode()) % SIZE; |
| 22 | if(buckets[index] == null) |
| 23 | buckets[index] = new LinkedList<>(); |
| 24 | LinkedList<MapEntry<K, V>> bucket = buckets[index]; |
| 25 | MapEntry<K, V> pair = new MapEntry<>(key, value); |
| 26 | boolean found = false; |
| 27 | ListIterator<MapEntry<K, V>> it = |
| 28 | bucket.listIterator(); |
| 29 | while(it.hasNext()) { |
| 30 | MapEntry<K, V> iPair = it.next(); |
| 31 | if(iPair.getKey().equals(key)) { |
| 32 | oldValue = iPair.getValue(); |
| 33 | it.set(pair); // Replace old with new |
| 34 | found = true; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | if(!found) |
| 39 | buckets[index].add(pair); |
| 40 | return oldValue; |
| 41 | } |
| 42 | @Override public V get(Object key) { |
| 43 | int index = Math.abs(key.hashCode()) % SIZE; |
| 44 | if(buckets[index] == null) return null; |