< prev index next >

src/java.base/share/classes/java/util/HashMap.java

Print this page




1065             ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
1066             e.value = newValue;
1067             afterNodeAccess(e);
1068             return true;
1069         }
1070         return false;
1071     }
1072 
1073     @Override
1074     public V replace(K key, V value) {
1075         Node<K,V> e;
1076         if ((e = getNode(hash(key), key)) != null) {
1077             V oldValue = e.value;
1078             e.value = value;
1079             afterNodeAccess(e);
1080             return oldValue;
1081         }
1082         return null;
1083     }
1084 







1085     @Override
1086     public V computeIfAbsent(K key,
1087                              Function<? super K, ? extends V> mappingFunction) {
1088         if (mappingFunction == null)
1089             throw new NullPointerException();
1090         int hash = hash(key);
1091         Node<K,V>[] tab; Node<K,V> first; int n, i;
1092         int binCount = 0;
1093         TreeNode<K,V> t = null;
1094         Node<K,V> old = null;
1095         if (size > threshold || (tab = table) == null ||
1096             (n = tab.length) == 0)
1097             n = (tab = resize()).length;
1098         if ((first = tab[i = (n - 1) & hash]) != null) {
1099             if (first instanceof TreeNode)
1100                 old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
1101             else {
1102                 Node<K,V> e = first; K k;
1103                 do {
1104                     if (e.hash == hash &&
1105                         ((k = e.key) == key || (key != null && key.equals(k)))) {
1106                         old = e;
1107                         break;
1108                     }
1109                     ++binCount;
1110                 } while ((e = e.next) != null);
1111             }
1112             V oldValue;
1113             if (old != null && (oldValue = old.value) != null) {
1114                 afterNodeAccess(old);
1115                 return oldValue;
1116             }
1117         }

1118         V v = mappingFunction.apply(key);

1119         if (v == null) {
1120             return null;
1121         } else if (old != null) {
1122             old.value = v;
1123             afterNodeAccess(old);
1124             return v;
1125         }
1126         else if (t != null)
1127             t.putTreeVal(this, tab, hash, key, v);
1128         else {
1129             tab[i] = newNode(hash, key, v, first);
1130             if (binCount >= TREEIFY_THRESHOLD - 1)
1131                 treeifyBin(tab, hash);
1132         }
1133         ++modCount;
1134         ++size;
1135         afterNodeInsertion(true);
1136         return v;
1137     }
1138 
1139     public V computeIfPresent(K key,
1140                               BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1141         if (remappingFunction == null)
1142             throw new NullPointerException();
1143         Node<K,V> e; V oldValue;
1144         int hash = hash(key);
1145         if ((e = getNode(hash, key)) != null &&
1146             (oldValue = e.value) != null) {
1147             V v = remappingFunction.apply(key, oldValue);
1148             if (v != null) {
1149                 e.value = v;
1150                 afterNodeAccess(e);
1151                 return v;
1152             }
1153             else




1065             ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
1066             e.value = newValue;
1067             afterNodeAccess(e);
1068             return true;
1069         }
1070         return false;
1071     }
1072 
1073     @Override
1074     public V replace(K key, V value) {
1075         Node<K,V> e;
1076         if ((e = getNode(hash(key), key)) != null) {
1077             V oldValue = e.value;
1078             e.value = value;
1079             afterNodeAccess(e);
1080             return oldValue;
1081         }
1082         return null;
1083     }
1084 
1085     /**
1086      * {@inheritDoc}
1087      * 
1088      * If the function itself causes a structural modification to the map, a
1089      * ConcurrentModificationException will be thrown.  As with iterators, this
1090      * exception is thrown on a best-effort basis.
1091      */
1092     @Override
1093     public V computeIfAbsent(K key,
1094                              Function<? super K, ? extends V> mappingFunction) {
1095         if (mappingFunction == null)
1096             throw new NullPointerException();
1097         int hash = hash(key);
1098         Node<K,V>[] tab; Node<K,V> first; int n, i;
1099         int binCount = 0;
1100         TreeNode<K,V> t = null;
1101         Node<K,V> old = null;
1102         if (size > threshold || (tab = table) == null ||
1103             (n = tab.length) == 0)
1104             n = (tab = resize()).length;
1105         if ((first = tab[i = (n - 1) & hash]) != null) {
1106             if (first instanceof TreeNode)
1107                 old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
1108             else {
1109                 Node<K,V> e = first; K k;
1110                 do {
1111                     if (e.hash == hash &&
1112                         ((k = e.key) == key || (key != null && key.equals(k)))) {
1113                         old = e;
1114                         break;
1115                     }
1116                     ++binCount;
1117                 } while ((e = e.next) != null);
1118             }
1119             V oldValue;
1120             if (old != null && (oldValue = old.value) != null) {
1121                 afterNodeAccess(old);
1122                 return oldValue;
1123             }
1124         }
1125         int mc = modCount;
1126         V v = mappingFunction.apply(key);
1127         if (mc != modCount) { throw new ConcurrentModificationException(); }
1128         if (v == null) {
1129             return null;
1130         } else if (old != null) {
1131             old.value = v;
1132             afterNodeAccess(old);
1133             return v;
1134         }
1135         else if (t != null)
1136             t.putTreeVal(this, tab, hash, key, v);
1137         else {
1138             tab[i] = newNode(hash, key, v, first);
1139             if (binCount >= TREEIFY_THRESHOLD - 1)
1140                 treeifyBin(tab, hash);
1141         }
1142         modCount = mc + 1;
1143         ++size;
1144         afterNodeInsertion(true);
1145         return v;
1146     }
1147 
1148     public V computeIfPresent(K key,
1149                               BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1150         if (remappingFunction == null)
1151             throw new NullPointerException();
1152         Node<K,V> e; V oldValue;
1153         int hash = hash(key);
1154         if ((e = getNode(hash, key)) != null &&
1155             (oldValue = e.value) != null) {
1156             V v = remappingFunction.apply(key, oldValue);
1157             if (v != null) {
1158                 e.value = v;
1159                 afterNodeAccess(e);
1160                 return v;
1161             }
1162             else


< prev index next >