src/share/classes/java/util/Hashtable.java

Print this page
rev 7313 : 8016446: Add override forEach/replaceAll to HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap
Reviewed-by: forax, duigou
Contributed-by: Mike Duigou <mike.duigou@oracle.com>, Remi Forax <forax@univ-mlv.fr>


 927         V result = get(key);
 928         return (null == result) ? defaultValue : result;
 929     }
 930 
 931     @Override
 932     public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
 933         Objects.requireNonNull(action);     // explicit check required in case
 934                                             // table is empty.
 935         Entry<?,?>[] tab = table;
 936         for (Entry<?,?> entry : tab) {
 937             while (entry != null) {
 938                 action.accept((K)entry.key, (V)entry.value);
 939                 entry = entry.next;
 940             }
 941         }
 942     }
 943 
 944     @Override
 945     public synchronized void replaceAll(
 946             BiFunction<? super K, ? super V, ? extends V> function) {
 947         Map.super.replaceAll(function);









 948     }
 949 
 950     @Override
 951     public synchronized V putIfAbsent(K key, V value) {
 952         Objects.requireNonNull(value);
 953 
 954         // Makes sure the key is not already in the hashtable.
 955         Entry<?,?> tab[] = table;
 956         int hash = hash(key);
 957         int index = (hash & 0x7FFFFFFF) % tab.length;
 958         @SuppressWarnings("unchecked")
 959         Entry<K,V> entry = (Entry<K,V>)tab[index];
 960         for (; entry != null; entry = entry.next) {
 961             if ((entry.hash == hash) && entry.key.equals(key)) {
 962                 V old = entry.value;
 963                 if (old == null) {
 964                     entry.value = value;
 965                 }
 966                 return old;
 967             }


1041         int hash = hash(key);
1042         int index = (hash & 0x7FFFFFFF) % tab.length;
1043         @SuppressWarnings("unchecked")
1044         Entry<K,V> e = (Entry<K,V>)tab[index];
1045         for (; e != null; e = e.next) {
1046             if (e.hash == hash && e.key.equals(key)) {
1047                 // Hashtable not accept null value
1048                 return e.value;
1049             }
1050         }
1051 
1052         V newValue = mappingFunction.apply(key);
1053         if (newValue != null) {
1054             addEntry(hash, key, newValue, index);
1055         }
1056 
1057         return newValue;
1058     }
1059 
1060     @Override
1061     public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1062         Objects.requireNonNull(remappingFunction);
1063 
1064         Entry<?,?> tab[] = table;
1065         int hash = hash(key);
1066         int index = (hash & 0x7FFFFFFF) % tab.length;
1067         @SuppressWarnings("unchecked")
1068         Entry<K,V> e = (Entry<K,V>)tab[index];
1069         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1070             if (e.hash == hash && e.key.equals(key)) {
1071                 V newValue = remappingFunction.apply(key, e.value);
1072                 if (newValue == null) {
1073                     modCount++;
1074                     if (prev != null) {
1075                         prev.next = e.next;
1076                     } else {
1077                         tab[index] = e.next;
1078                     }
1079                     count--;
1080                 } else {
1081                     e.value = newValue;
1082                 }
1083                 return newValue;
1084             }
1085         }
1086         return null;
1087     }
1088 
1089     @Override
1090     public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1091         Objects.requireNonNull(remappingFunction);
1092 
1093         Entry<?,?> tab[] = table;
1094         int hash = hash(key);
1095         int index = (hash & 0x7FFFFFFF) % tab.length;
1096         @SuppressWarnings("unchecked")
1097         Entry<K,V> e = (Entry<K,V>)tab[index];
1098         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1099             if (e.hash == hash && Objects.equals(e.key, key)) {
1100                 V newValue = remappingFunction.apply(key, e.value);
1101                 if (newValue == null) {
1102                     modCount++;
1103                     if (prev != null) {
1104                         prev.next = e.next;
1105                     } else {
1106                         tab[index] = e.next;
1107                     }
1108                     count--;
1109                 } else {
1110                     e.value = newValue;
1111                 }
1112                 return newValue;
1113             }
1114         }
1115 
1116         V newValue = remappingFunction.apply(key, null);
1117         if (newValue != null) {
1118             addEntry(hash, key, newValue, index);
1119         }
1120 
1121         return newValue;
1122     }
1123 
1124     @Override
1125     public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1126         Objects.requireNonNull(remappingFunction);
1127 
1128         Entry<?,?> tab[] = table;
1129         int hash = hash(key);
1130         int index = (hash & 0x7FFFFFFF) % tab.length;
1131         @SuppressWarnings("unchecked")
1132         Entry<K,V> e = (Entry<K,V>)tab[index];
1133         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1134             if (e.hash == hash && e.key.equals(key)) {
1135                 V newValue = remappingFunction.apply(e.value, value);
1136                 if (newValue == null) {
1137                     modCount++;
1138                     if (prev != null) {
1139                         prev.next = e.next;
1140                     } else {
1141                         tab[index] = e.next;
1142                     }
1143                     count--;
1144                 } else {
1145                     e.value = newValue;




 927         V result = get(key);
 928         return (null == result) ? defaultValue : result;
 929     }
 930 
 931     @Override
 932     public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
 933         Objects.requireNonNull(action);     // explicit check required in case
 934                                             // table is empty.
 935         Entry<?,?>[] tab = table;
 936         for (Entry<?,?> entry : tab) {
 937             while (entry != null) {
 938                 action.accept((K)entry.key, (V)entry.value);
 939                 entry = entry.next;
 940             }
 941         }
 942     }
 943 
 944     @Override
 945     public synchronized void replaceAll(
 946             BiFunction<? super K, ? super V, ? extends V> function) {
 947         Objects.requireNonNull(function);     // explicit check required in case
 948                                               // table is empty.
 949         Entry<K,V>[] tab = (Entry<K,V>[]) table;
 950         for (Entry<K,V> entry : tab) {
 951             while (entry != null) {
 952                 entry.value = Objects.requireNonNull(
 953                     function.apply((K)entry.key, (V)entry.value));
 954                 entry = entry.next;
 955             }
 956         }
 957     }
 958 
 959     @Override
 960     public synchronized V putIfAbsent(K key, V value) {
 961         Objects.requireNonNull(value);
 962 
 963         // Makes sure the key is not already in the hashtable.
 964         Entry<?,?> tab[] = table;
 965         int hash = hash(key);
 966         int index = (hash & 0x7FFFFFFF) % tab.length;
 967         @SuppressWarnings("unchecked")
 968         Entry<K,V> entry = (Entry<K,V>)tab[index];
 969         for (; entry != null; entry = entry.next) {
 970             if ((entry.hash == hash) && entry.key.equals(key)) {
 971                 V old = entry.value;
 972                 if (old == null) {
 973                     entry.value = value;
 974                 }
 975                 return old;
 976             }


1050         int hash = hash(key);
1051         int index = (hash & 0x7FFFFFFF) % tab.length;
1052         @SuppressWarnings("unchecked")
1053         Entry<K,V> e = (Entry<K,V>)tab[index];
1054         for (; e != null; e = e.next) {
1055             if (e.hash == hash && e.key.equals(key)) {
1056                 // Hashtable not accept null value
1057                 return e.value;
1058             }
1059         }
1060 
1061         V newValue = mappingFunction.apply(key);
1062         if (newValue != null) {
1063             addEntry(hash, key, newValue, index);
1064         }
1065 
1066         return newValue;
1067     }
1068 
1069     @Override
1070     public synchronized V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1071         Objects.requireNonNull(remappingFunction);
1072 
1073         Entry<?,?> tab[] = table;
1074         int hash = hash(key);
1075         int index = (hash & 0x7FFFFFFF) % tab.length;
1076         @SuppressWarnings("unchecked")
1077         Entry<K,V> e = (Entry<K,V>)tab[index];
1078         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1079             if (e.hash == hash && e.key.equals(key)) {
1080                 V newValue = remappingFunction.apply(key, e.value);
1081                 if (newValue == null) {
1082                     modCount++;
1083                     if (prev != null) {
1084                         prev.next = e.next;
1085                     } else {
1086                         tab[index] = e.next;
1087                     }
1088                     count--;
1089                 } else {
1090                     e.value = newValue;
1091                 }
1092                 return newValue;
1093             }
1094         }
1095         return null;
1096     }
1097 
1098     @Override
1099     public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1100         Objects.requireNonNull(remappingFunction);
1101 
1102         Entry<?,?> tab[] = table;
1103         int hash = hash(key);
1104         int index = (hash & 0x7FFFFFFF) % tab.length;
1105         @SuppressWarnings("unchecked")
1106         Entry<K,V> e = (Entry<K,V>)tab[index];
1107         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1108             if (e.hash == hash && Objects.equals(e.key, key)) {
1109                 V newValue = remappingFunction.apply(key, e.value);
1110                 if (newValue == null) {
1111                     modCount++;
1112                     if (prev != null) {
1113                         prev.next = e.next;
1114                     } else {
1115                         tab[index] = e.next;
1116                     }
1117                     count--;
1118                 } else {
1119                     e.value = newValue;
1120                 }
1121                 return newValue;
1122             }
1123         }
1124 
1125         V newValue = remappingFunction.apply(key, null);
1126         if (newValue != null) {
1127             addEntry(hash, key, newValue, index);
1128         }
1129 
1130         return newValue;
1131     }
1132 
1133     @Override
1134     public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1135         Objects.requireNonNull(remappingFunction);
1136 
1137         Entry<?,?> tab[] = table;
1138         int hash = hash(key);
1139         int index = (hash & 0x7FFFFFFF) % tab.length;
1140         @SuppressWarnings("unchecked")
1141         Entry<K,V> e = (Entry<K,V>)tab[index];
1142         for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
1143             if (e.hash == hash && e.key.equals(key)) {
1144                 V newValue = remappingFunction.apply(e.value, value);
1145                 if (newValue == null) {
1146                     modCount++;
1147                     if (prev != null) {
1148                         prev.next = e.next;
1149                     } else {
1150                         tab[index] = e.next;
1151                     }
1152                     count--;
1153                 } else {
1154                     e.value = newValue;