作者:博士吃货 | 来源:互联网 | 2023-06-24 09:03
对着HashMap进行比较https:www.cnblogs.comwqff-biubiup8963817.htmlConcurrentHashMap与HashMap实现的接口相同
对着HashMap进行比较https://www.cnblogs.com/wqff-biubiu/p/8963817.html
ConcurrentHashMap与HashMap实现的接口
相同:AbstractMap,Serializable,Map----->可序列化的键值对
差别:ConcurrentHashMap比HashMap
- 少实现了Cloneable接口---------不支持clone()
- 多实现了ConcurrentMap接口------------重写并添加了部分方法
ConcurrentMap接口的作用:
@Override
default V getOrDefault(Object key, V defaultValue) {
V v;
return ((v = get(key)) != null) ? v : defaultValue;
//Map -- return (((v = get(key)) != null) || containsKey(key))? v: defaultValue;//重写了get(key)+containKey(key),containKey(key)-->getKey()!=null
}
@Override
default void forEach(BiConsumer super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
continue;
//Map -- throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
V putIfAbsent(K key, V value);
boolean remove(Object key, Object value);
boolean replace(K key, V oldValue, V newValue);
V replace(K key, V value);
@Override
default void replaceAll(BiFunction super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
forEach((k,v) -> {
while(!replace(k, v, function.apply(k, v))) {
// v changed or k is gone
if ( (v = get(k)) == null) {
// k is no longer in the map.
break;
}
}
//Map -- forEach--entry.set(function.apply(k, v))
});
}
@Override
default V computeIfAbsent(K key,
Function super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v, newValue;
return ((v = get(key)) == null &&
(newValue = mappingFunction.apply(key)) != null &&
(v = putIfAbsent(key, newValue)) == null) ? newValue : v;
//Map put(key, newValue); not putIfAbsent(key, newValue))
}
@Override
default V computeIfPresent(K key,
BiFunction super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue;
while((oldValue = get(key)) != null) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null) {
if (replace(key, oldValue, newValue))
//Map put(key, newValue);
return newValue;
} else if (remove(key, oldValue))
//Map remove(key);
return null;
}
return oldValue;
}
@Override
default V compute(K key,
BiFunction super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue = get(key);
for(;;) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue == null) {
// delete mapping
if (oldValue != null || containsKey(key)) {
// something to remove
if (remove(key, oldValue)) {
//Map remove(key);
// removed the old value as expected
return null;
}
// some other value replaced old value. try again.
oldValue = get(key);
} else {
// nothing to do. Leave things as they were.
return null;
}
} else {
// add or replace old mapping
//Map put(key, newValue);
if (oldValue != null) {
// replace
if (replace(key, oldValue, newValue)) {
// replaced as expected.
return newValue;
}
// some other value replaced old value. try again.
oldValue = get(key);
} else {
// add (replace if oldValue was null)
if ((oldValue = putIfAbsent(key, newValue)) == null) {
// replaced
return newValue;
}
// some other value replaced old value. try again.
}
}
}
}
@Override
default V merge(K key, V value,
BiFunction super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
for (;;) {
if (oldValue != null) {
V newValue = remappingFunction.apply(oldValue, value);
if (newValue != null) {
if (replace(key, oldValue, newValue))
//Map put(key, newValue);
return newValue;
} else if (remove(key, oldValue)) {
// Map remove(key);
return null;
}
oldValue = get(key);
} else {
if ((oldValue = putIfAbsent(key, value)) == null) {
return value;
}
}
}
}