MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / put

Method put

equalshashcode/SimpleHashMap.java:19–41  ·  view source on GitHub ↗
(K key, V value)

Source from the content-addressed store, hash-verified

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;

Callers

nothing calls this directly

Calls 8

nextMethod · 0.65
setMethod · 0.65
hashCodeMethod · 0.45
hasNextMethod · 0.45
equalsMethod · 0.45
getKeyMethod · 0.45
getValueMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected