コード例 #1
0
 public V get(Object key) {
   int index = Math.abs(key.hashCode()) % SIZE;
   if (buckets[index] == null) return null;
   for (MapEntry<K, V> iPair : buckets[index])
     if (iPair.getKey().equals(key)) {
       return iPair.getValue();
     }
   return null;
 }
コード例 #2
0
 public V remove(Object k) {
   int index = Math.abs(k.hashCode()) % SIZE;
   LinkedList<MapEntry<K, V>> bucket = buckets[index];
   if (bucket == null) return null;
   ListIterator<MapEntry<K, V>> it = bucket.listIterator();
   while (it.hasNext()) {
     MapEntry<K, V> iPair = it.next();
     if (iPair.getKey().equals(k)) {
       V value = iPair.getValue();
       it.remove();
       return value;
     }
   }
   return null;
 }
コード例 #3
0
 public V put(K key, V value) {
   V oldValue = null;
   int index = Math.abs(key.hashCode()) % SIZE;
   if (buckets[index] == null) buckets[index] = new LinkedList<>();
   LinkedList<MapEntry<K, V>> bucket = buckets[index];
   MapEntry<K, V> pair = new MapEntry<>(key, value);
   boolean found = false;
   ListIterator<MapEntry<K, V>> it = bucket.listIterator();
   while (it.hasNext()) {
     MapEntry<K, V> iPair = it.next();
     if (iPair.getKey().equals(key)) {
       oldValue = iPair.getValue();
       it.set(pair); // Replace old with new
       found = true;
       break;
     }
   }
   if (!found) buckets[index].add(pair);
   return oldValue;
 }