Java : ConcurrentHashMap (並列処理用マップ) - API使用例
ConcurrentHashMap (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ConcurrentHashMap クラスは、反復処理 (例えば for-eachループ文) 中に put や remove などによる変更を許容する Map の実装です。
非チェック例外の ConcurrentModificationException が発生しないことが保証されています。
汎用コレクションの HashMap に相当します。
void test(Map<String, Integer> map) throws InterruptedException {
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
try (final var executor = Executors.newSingleThreadScheduledExecutor()) {
executor.schedule(() -> {
System.out.println("-- put entry! --");
map.put("X", 999);
}, 5, TimeUnit.SECONDS);
try {
for (final var entry : map.entrySet()) {
System.out.println("entry = " + entry);
TimeUnit.SECONDS.sleep(2);
}
} catch (ConcurrentModificationException e) {
System.out.println("ConcurrentModificationException!");
}
}
System.out.println("-- end --");
System.out.println("map = " + map);
}
test(new HashMap<>());
// 結果
// ↓
//entry = a=10
//entry = b=20
//entry = c=30
//-- put entry! --
//ConcurrentModificationException!
//-- end --
//map = {a=10, b=20, c=30, d=40, e=50, X=999}
test(new ConcurrentHashMap<>());
// 結果
// ↓
//entry = a=10
//entry = b=20
//entry = c=30
//-- put entry! --
//entry = d=40
//entry = e=50
//entry = X=999
//-- end --
//map = {a=10, b=20, c=30, d=40, e=50, X=999}
コンストラクタ
ConcurrentHashMap ()
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.isEmpty()); // true
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.size()); // 3
ConcurrentHashMap (int initialCapacity)
初期容量(initialCapacity) を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
final var map = new ConcurrentHashMap<String, Integer>(10000000);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 0.890473 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new ConcurrentHashMap<String, Integer>(1);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 1.619073 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
ConcurrentHashMap (int initialCapacity, float loadFactor)
負荷係数(loadFactor) を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
関連:
final var map = new ConcurrentHashMap<String, Integer>(16, 0.000001f);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 0.817980 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new ConcurrentHashMap<String, Integer>(16, 0.75f);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 1.649406 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel)
concurrentLevel を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
関連:
- ConcurrentHashMap()
- ConcurrentHashMap(int initialCapacity)
- ConcurrentHashMap(int initialCapacity, float loadFactor)
final var map = new ConcurrentHashMap<String, Integer>(16, 0.75f, 10000000);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 0.872350 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new ConcurrentHashMap<String, Integer>(16, 0.75f, 1);
System.out.println(map); // {}
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
final var key = String.valueOf(i);
map.put(key, i);
map.put(key, map.get(key) + 1);
}
final var endTime = System.nanoTime();
// 1.714618 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
ConcurrentHashMap (Map<? extends K,? extends V> m)
final var m = Map.of("a", 10, "b", 20, "c", 30);
final var map = new ConcurrentHashMap<>(m);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.size()); // 3
メソッド
void clear ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
V compute (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
if (value == null) {
return -100;
} else {
return value * 10;
}
}
};
final var ret1 = map.compute("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}
final var ret2 = map.compute("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}
final var ret3 = map.compute("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}
final var ret4 = map.compute("X", func);
System.out.println(ret4); // -100
System.out.println(map); // {a=10, b=20, c=30, X=-100}
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var ret = map.compute("b", (key, value) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}
V computeIfAbsent (K key, Function<? super K,? extends V> mappingFunction)
final var map = new ConcurrentHashMap<String, String>();
map.put("a", "aaa");
map.put("b", "bbb");
System.out.println(map); // {a=aaa, b=bbb}
final var func = new Function<String, String>() {
@Override
public String apply(String key) {
if ("c".equals(key)) {
return "ccc";
} else {
return null;
}
}
};
final var ret1 = map.computeIfAbsent("a", func);
System.out.println(ret1); // aaa
System.out.println(map); // {a=aaa, b=bbb}
final var ret2 = map.computeIfAbsent("b", func);
System.out.println(ret2); // bbb
System.out.println(map); // {a=aaa, b=bbb}
final var ret3 = map.computeIfAbsent("c", func);
System.out.println(ret3); // ccc
System.out.println(map); // {a=aaa, b=bbb, c=ccc}
final var ret4 = map.computeIfAbsent("d", func);
System.out.println(ret4); // null
System.out.println(map); // {a=aaa, b=bbb, c=ccc}
// valueにListを持たせる例です。
final var map = new ConcurrentHashMap<String, List<String>>();
System.out.println(map); // {}
// 最初のキー追加のときにArrayListを作成するようにします。
map.computeIfAbsent("a", (key) -> new ArrayList<>()).add("a-1");
System.out.println(map); // {a=[a-1]}
map.computeIfAbsent("a", (key) -> new ArrayList<>()).add("a-2");
System.out.println(map); // {a=[a-1, a-2]}
map.computeIfAbsent("b", (key) -> new ArrayList<>()).add("b-1");
System.out.println(map); // {a=[a-1, a-2], b=[b-1]}
V computeIfPresent (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
return value * 10;
}
};
final var ret1 = map.computeIfPresent("a", func);
System.out.println(ret1); // 10
System.out.println(map); // {a=10, b=2, c=3}
final var ret2 = map.computeIfPresent("b", func);
System.out.println(ret2); // 20
System.out.println(map); // {a=10, b=20, c=3}
final var ret3 = map.computeIfPresent("c", func);
System.out.println(ret3); // 30
System.out.println(map); // {a=10, b=20, c=30}
final var ret4 = map.computeIfPresent("X", func);
System.out.println(ret4); // null
System.out.println(map); // {a=10, b=20, c=30}
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var ret = map.computeIfPresent("b", (k, v) -> null);
System.out.println(ret); // null
System.out.println(map); // {a=1, c=3}
boolean contains (Object value)
containsValue(Object value) と同等です。
API使用例はそちらをご参照ください。
boolean containsKey (Object key)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.containsKey("a")); // true
System.out.println(map.containsKey("b")); // true
System.out.println(map.containsKey("c")); // true
System.out.println(map.containsKey("X")); // false
System.out.println(map.containsKey("")); // false
boolean containsValue (Object value)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("X", 3);
System.out.println(map); // {a=1, b=2, c=3, X=3}
System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(1)); // true
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // true
System.out.println(map.containsValue(4)); // false
Enumeration<V> elements ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
final var elements = map.elements();
System.out.println("-- elements --");
while (elements.hasMoreElements()) {
final var element = elements.nextElement();
System.out.println(element);
}
// 結果
// ↓
//-- elements --
//10
//20
//30
Set<Map.Entry<K,V>> entrySet ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var entries = map.entrySet();
System.out.println(entries); // [a=1, b=2, c=3]
map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(entries); // [a=1, b=20, c=3]
entries.remove(Map.entry("a", 1));
System.out.println(map); // {b=20, c=3}
System.out.println(entries); // [b=20, c=3]
for (final var entry : entries) {
entry.setValue(entry.getValue() * 10);
}
System.out.println(map); // {b=200, c=30}
System.out.println(entries); // [b=200, c=30]
boolean equals (Object o)
final var map1 = new ConcurrentHashMap<String, Integer>();
map1.put("a", 1);
map1.put("b", 2);
map1.put("c", 3);
System.out.println(map1); // {a=1, b=2, c=3}
final var map2 = new ConcurrentHashMap<String, Integer>();
map2.put("a", 1);
map2.put("b", 2);
map2.put("c", 3);
System.out.println(map2); // {a=1, b=2, c=3}
final var map3 = new ConcurrentHashMap<String, Integer>();
map3.put("A", 1);
map3.put("B", 2);
map3.put("C", 3);
System.out.println(map3); // {A=1, B=2, C=3}
final var map4 = new ConcurrentHashMap<String, Integer>();
map4.put("a", 10);
map4.put("b", 20);
map4.put("c", 30);
System.out.println(map4); // {a=10, b=20, c=30}
System.out.println(map1.equals(map2)); // true
System.out.println(map1.equals(map3)); // false
System.out.println(map1.equals(map4)); // false
void forEach (long parallelismThreshold, BiConsumer<? super K,? super V> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEach(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s = %d (tid = %d)%n", key, value, tid);
});
// 結果
// ↓
//-- forEach --
//a = 10 (tid = 1)
//b = 20 (tid = 1)
//c = 30 (tid = 1)
//d = 40 (tid = 33)
//e = 50 (tid = 33)
<U> void forEach (long parallelismThreshold, BiFunction<? super K,? super V,? extends U> transformer, Consumer<? super U> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEach(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s = %d (tid = %d)%n", key, value, tid);
return value * 10;
}, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("action : %s (tid = %d)%n", value, tid);
});
// 結果
// ↓
//-- forEach --
//transformer : a = 10 (tid = 1)
//action : 100 (tid = 1)
//transformer : b = 20 (tid = 1)
//action : 200 (tid = 1)
//transformer : c = 30 (tid = 1)
//action : 300 (tid = 1)
//transformer : d = 40 (tid = 33)
//action : 400 (tid = 33)
//transformer : e = 50 (tid = 33)
//action : 500 (tid = 33)
void forEachEntry (long parallelismThreshold, Consumer<? super Map.Entry<K,V>> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachEntry(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s (tid = %d)%n", entry, tid);
});
// 結果
// ↓
//-- forEach --
//a=10 (tid = 1)
//b=20 (tid = 1)
//c=30 (tid = 1)
//d=40 (tid = 33)
//e=50 (tid = 33)
<U> void forEachEntry (long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> transformer, Consumer<? super U> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachEntry(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", entry, tid);
return entry.getValue() * 10;
}, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("action : %s (tid = %d)%n", value, tid);
});
// 結果
// ↓
//-- forEach --
//transformer : a=10 (tid = 1)
//action : 100 (tid = 1)
//transformer : b=20 (tid = 1)
//action : 200 (tid = 1)
//transformer : c=30 (tid = 1)
//action : 300 (tid = 1)
//transformer : d=40 (tid = 33)
//action : 400 (tid = 33)
//transformer : e=50 (tid = 33)
//action : 500 (tid = 33)
void forEachKey (long parallelismThreshold, Consumer<? super K> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachKey(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s (tid = %d)%n", key, tid);
});
// 結果
// ↓
//-- forEach --
//a (tid = 1)
//b (tid = 1)
//c (tid = 1)
//d (tid = 33)
//e (tid = 33)
<U> void forEachKey (long parallelismThreshold, Function<? super K,? extends U> transformer, Consumer<? super U> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachKey(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", key, tid);
return key.toUpperCase();
}, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("action : %s (tid = %d)%n", value, tid);
});
// 結果
// ↓
//-- forEach --
//transformer : a (tid = 1)
//action : A (tid = 1)
//transformer : b (tid = 1)
//action : B (tid = 1)
//transformer : c (tid = 1)
//action : C (tid = 1)
//transformer : d (tid = 33)
//action : D (tid = 33)
//transformer : e (tid = 33)
//action : E (tid = 33)
void forEachValue (long parallelismThreshold, Consumer<? super V> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachValue(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s (tid = %d)%n", value, tid);
});
// 結果
// ↓
//-- forEach --
//10 (tid = 1)
//20 (tid = 1)
//30 (tid = 1)
//40 (tid = 33)
//50 (tid = 33)
<U> void forEachValue (long parallelismThreshold, Function<? super V,? extends U> transformer, Consumer<? super U> action)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- forEach --");
map.forEachValue(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", value, tid);
return value * 10;
}, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("action : %s (tid = %d)%n", value, tid);
});
// 結果
// ↓
//-- forEach --
//transformer : 10 (tid = 1)
//action : 100 (tid = 1)
//transformer : 20 (tid = 1)
//action : 200 (tid = 1)
//transformer : 30 (tid = 1)
//action : 300 (tid = 1)
//transformer : 40 (tid = 33)
//action : 400 (tid = 33)
//transformer : 50 (tid = 33)
//action : 500 (tid = 33)
V get (Object key)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.get("a")); // 1
System.out.println(map.get("b")); // 2
System.out.println(map.get("c")); // 3
System.out.println(map.get("X")); // null
System.out.println(map.get("")); // null
V getOrDefault (Object key, V defaultValue)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.getOrDefault("a", 10)); // 1
System.out.println(map.getOrDefault("b", 20)); // 2
System.out.println(map.getOrDefault("c", 30)); // 3
System.out.println(map.getOrDefault("X", 40)); // 40
System.out.println(map.getOrDefault("", 50)); // 50
int hashCode ()
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.hashCode()); // 0
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.hashCode()); // 192
final var map = new ConcurrentHashMap<String, Integer>();
map.put("A", 1);
map.put("B", 2);
System.out.println(map); // {A=1, B=2}
System.out.println(map.hashCode()); // 128
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
System.out.println(map); // {a=10, b=20}
System.out.println(map.hashCode()); // 225
boolean isEmpty ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
System.out.println(map.isEmpty()); // false
map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0
System.out.println(map.isEmpty()); // true
Enumeration<K> keys ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
final var keys = map.keys();
System.out.println("-- keys --");
while (keys.hasMoreElements()) {
final var element = keys.nextElement();
System.out.println(element);
}
// 結果
// ↓
//-- keys --
//a
//b
//c
ConcurrentHashMap.KeySetView<K,V> keySet ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var keys = map.keySet();
System.out.println(keys); // [a, b, c]
map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]
keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]
ConcurrentHashMap.KeySetView<K,V> keySet (V mappedValue)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var keys = map.keySet(999);
System.out.println(keys); // [a, b, c]
map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]
keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]
keys.add("X");
System.out.println(map); // {b=2, c=3, d=4, X=999}
System.out.println(keys); // [b, c, d, X]
long mappingCount ()
size() の代わりに mappingCount() を使うことが推奨されています。
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.mappingCount()); // 0
map.put("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.mappingCount()); // 1
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.mappingCount()); // 2
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.mappingCount()); // 3
V merge (K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var func = new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer oldValue, Integer newValue) {
return oldValue + newValue;
}
};
final var ret1 = map.merge("a", 1000, func);
System.out.println(ret1); // 1001
System.out.println(map); // {a=1001, b=2, c=3}
final var ret2 = map.merge("b", 1000, func);
System.out.println(ret2); // 1002
System.out.println(map); // {a=1001, b=1002, c=3}
final var ret3 = map.merge("c", 1000, func);
System.out.println(ret3); // 1003
System.out.println(map); // {a=1001, b=1002, c=1003}
final var ret4 = map.merge("d", 1000, func);
System.out.println(ret4); // 1000
System.out.println(map); // {a=1001, b=1002, c=1003, d=1000}
final var ret5 = map.merge("b", 1000, (k, v) -> null);
System.out.println(ret5); // null
System.out.println(map); // {a=1001, c=1003, d=1000}
static <K> ConcurrentHashMap.KeySetView<K,Boolean> newKeySet ()
final var set = ConcurrentHashMap.<String>newKeySet();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
static <K> ConcurrentHashMap.KeySetView<K,Boolean> newKeySet (int initialCapacity)
初期容量(initialCapacity) を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
関連:newKeySet()
final var set = ConcurrentHashMap.<String>newKeySet(10000000);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 0.641714 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var set = ConcurrentHashMap.<String>newKeySet(1);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 4000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 1.375524 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
V put (K key, V value)
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.put("a", 1)); // null
System.out.println(map); // {a=1}
System.out.println(map.put("b", 2)); // null
System.out.println(map); // {a=1, b=2}
System.out.println(map.put("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.put("a", 999)); // 1
System.out.println(map); // {a=999, b=2, c=3}
void putAll (Map<? extends K,? extends V> m)
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
map.putAll(Map.of());
System.out.println(map); // {}
map.putAll(Map.of("a", 1, "b", 2));
System.out.println(map); // {a=1, b=2}
map.putAll(Map.of("c", 3, "d", 4, "e", 5));
System.out.println(map); // {a=1, b=2, c=3, d=4, e=5}
map.putAll(Map.of("a", 901, "b", 902, "z", 100));
System.out.println(map); // {a=901, b=902, c=3, d=4, e=5, z=100}
V putIfAbsent (K key, V value)
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.putIfAbsent("a", 1)); // null
System.out.println(map); // {a=1}
System.out.println(map.putIfAbsent("b", 2)); // null
System.out.println(map); // {a=1, b=2}
System.out.println(map.putIfAbsent("c", 3)); // null
System.out.println(map); // {a=1, b=2, c=3}
// 上書きされません。
System.out.println(map.putIfAbsent("a", 999)); // 1
System.out.println(map); // {a=1, b=2, c=3}
<U> U reduce (long parallelismThreshold, BiFunction<? super K,? super V,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduce(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s = %d (tid = %d)%n", key, value, tid);
return key;
}, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a = 10 (tid = 1)
//transformer : b = 20 (tid = 1)
//transformer : c = 30 (tid = 1)
//transformer : d = 40 (tid = 33)
//transformer : e = 50 (tid = 33)
//reducer : d, e (tid = 33)
//reducer : b, c (tid = 1)
//reducer : a, bc (tid = 33)
//reducer : abc, de (tid = 33)
//----
//ret = abcde
Map.Entry<K,V> reduceEntries (long parallelismThreshold, BiFunction<Map.Entry<K,V>,Map.Entry<K,V>,? extends Map.Entry<K,V>> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceEntries(1, (e1, e2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", e1, e2, tid);
return Map.entry(e1.getKey() + e2.getKey(), e1.getValue() + e2.getValue());
});
System.out.println("----");
System.out.println("ret : " + ret);
// 結果
// ↓
//-- reduce --
//reducer : b=20, c=30 (tid = 1)
//reducer : d=40, e=50 (tid = 33)
//reducer : a=10, bc=50 (tid = 33)
//reducer : abc=60, de=90 (tid = 33)
//----
//ret : abcde=150
<U> U reduceEntries (long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceEntries(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", entry, tid);
return entry.getKey();
}, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a=10 (tid = 1)
//transformer : b=20 (tid = 1)
//transformer : c=30 (tid = 1)
//transformer : d=40 (tid = 33)
//transformer : e=50 (tid = 33)
//reducer : d, e (tid = 33)
//reducer : b, c (tid = 1)
//reducer : a, bc (tid = 1)
//reducer : abc, de (tid = 1)
//----
//ret = abcde
double reduceEntriesToDouble (long parallelismThreshold, ToDoubleFunction<Map.Entry<K,V>> transformer, double basis, DoubleBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Double>();
map.put("a", 10.0);
map.put("b", 20.0);
map.put("c", 30.0);
map.put("d", 40.0);
map.put("e", 50.0);
System.out.println(map); // {a=10.0, b=20.0, c=30.0, d=40.0, e=50.0}
System.out.println("-- reduce --");
final var ret = map.reduceEntriesToDouble(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", entry, tid);
return entry.getValue();
}, 0.0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %f, %f (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a=10.0 (tid = 1)
//reducer : 0.000000, 10.000000 (tid = 1)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//transformer : b=20.0 (tid = 33)
//reducer : 0.000000, 20.000000 (tid = 33)
//transformer : d=40.0 (tid = 34)
//reducer : 0.000000, 40.000000 (tid = 34)
//transformer : e=50.0 (tid = 34)
//reducer : 40.000000, 50.000000 (tid = 34)
//reducer : 90.000000, 0.000000 (tid = 34)
//transformer : c=30.0 (tid = 33)
//reducer : 20.000000, 30.000000 (tid = 33)
//reducer : 10.000000, 50.000000 (tid = 33)
//reducer : 60.000000, 90.000000 (tid = 33)
//reducer : 150.000000, 0.000000 (tid = 33)
//----
//ret = 150.0
int reduceEntriesToInt (long parallelismThreshold, ToIntFunction<Map.Entry<K,V>> transformer, int basis, IntBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceEntriesToInt(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", entry, tid);
return entry.getValue();
}, 0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a=10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//transformer : b=20 (tid = 1)
//reducer : 0, 20 (tid = 1)
//transformer : c=30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//transformer : d=40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//transformer : e=50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
long reduceEntriesToLong (long parallelismThreshold, ToLongFunction<Map.Entry<K,V>> transformer, long basis, LongBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Long>();
map.put("a", 10L);
map.put("b", 20L);
map.put("c", 30L);
map.put("d", 40L);
map.put("e", 50L);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceEntriesToLong(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", entry, tid);
return entry.getValue();
}, 0L, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a=10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//transformer : b=20 (tid = 34)
//reducer : 0, 20 (tid = 34)
//transformer : c=30 (tid = 34)
//reducer : 20, 30 (tid = 34)
//transformer : d=40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//transformer : e=50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
K reduceKeys (long parallelismThreshold, BiFunction<? super K,? super K,? extends K> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceKeys(1, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//reducer : b, c (tid = 1)
//reducer : d, e (tid = 33)
//reducer : a, bc (tid = 33)
//reducer : abc, de (tid = 33)
//----
//ret = abcde
<U> U reduceKeys (long parallelismThreshold, Function<? super K,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceKeys(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", key, tid);
return key.toUpperCase();
}, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a (tid = 1)
//transformer : b (tid = 1)
//transformer : c (tid = 1)
//transformer : d (tid = 33)
//transformer : e (tid = 33)
//reducer : D, E (tid = 33)
//reducer : B, C (tid = 1)
//reducer : A, BC (tid = 1)
//reducer : ABC, DE (tid = 1)
//----
//ret = ABCDE
double reduceKeysToDouble (long parallelismThreshold, ToDoubleFunction<? super K> transformer, double basis, DoubleBinaryOperator reducer)
final var map = new ConcurrentHashMap<Double, String>();
map.put(10.0, "a");
map.put(20.0, "b");
map.put(30.0, "c");
map.put(40.0, "d");
map.put(50.0, "e");
System.out.println(map); // {10.0=a, 20.0=b, 40.0=d, 50.0=e, 30.0=c}
System.out.println("-- reduce --");
final var ret = map.reduceKeysToDouble(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", key, tid);
return key;
}, 0.0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %f, %f (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 10.0 (tid = 1)
//reducer : 0.000000, 10.000000 (tid = 1)
//transformer : 20.0 (tid = 1)
//reducer : 10.000000, 20.000000 (tid = 1)
//transformer : 40.0 (tid = 1)
//reducer : 30.000000, 40.000000 (tid = 1)
//reducer : 70.000000, 0.000000 (tid = 1)
//transformer : 50.0 (tid = 32)
//reducer : 0.000000, 50.000000 (tid = 32)
//transformer : 30.0 (tid = 34)
//reducer : 0.000000, 30.000000 (tid = 34)
//reducer : 0.000000, 30.000000 (tid = 34)
//reducer : 50.000000, 0.000000 (tid = 34)
//reducer : 50.000000, 30.000000 (tid = 34)
//reducer : 0.000000, 0.000000 (tid = 34)
//reducer : 0.000000, 70.000000 (tid = 34)
//reducer : 70.000000, 80.000000 (tid = 34)
//----
//ret = 150.0
int reduceKeysToInt (long parallelismThreshold, ToIntFunction<? super K> transformer, int basis, IntBinaryOperator reducer)
final var map = new ConcurrentHashMap<Integer, String>();
map.put(10, "a");
map.put(20, "b");
map.put(30, "c");
map.put(40, "d");
map.put(50, "e");
System.out.println(map); // {50=e, 20=b, 40=d, 10=a, 30=c}
System.out.println("-- reduce --");
final var ret = map.reduceKeysToInt(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %d (tid = %d)%n", key, tid);
return key;
}, 0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 50 (tid = 1)
//reducer : 0, 50 (tid = 1)
//transformer : 40 (tid = 32)
//reducer : 0, 40 (tid = 32)
//transformer : 20 (tid = 33)
//reducer : 0, 20 (tid = 33)
//reducer : 20, 0 (tid = 33)
//transformer : 10 (tid = 34)
//reducer : 0, 10 (tid = 34)
//transformer : 30 (tid = 1)
//reducer : 0, 30 (tid = 1)
//reducer : 0, 30 (tid = 1)
//reducer : 40, 10 (tid = 1)
//reducer : 50, 30 (tid = 1)
//reducer : 0, 50 (tid = 1)
//reducer : 50, 20 (tid = 1)
//reducer : 70, 80 (tid = 1)
//----
//ret = 150
long reduceKeysToLong (long parallelismThreshold, ToLongFunction<? super K> transformer, long basis, LongBinaryOperator reducer)
final var map = new ConcurrentHashMap<Long, String>();
map.put(10L, "a");
map.put(20L, "b");
map.put(30L, "c");
map.put(40L, "d");
map.put(50L, "e");
System.out.println(map); // {50=e, 20=b, 40=d, 10=a, 30=c}
System.out.println("-- reduce --");
final var ret = map.reduceKeysToLong(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %d (tid = %d)%n", key, tid);
return key;
}, 0L, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 50 (tid = 1)
//reducer : 0, 50 (tid = 1)
//transformer : 40 (tid = 32)
//reducer : 0, 40 (tid = 32)
//transformer : 10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//transformer : 20 (tid = 33)
//reducer : 0, 20 (tid = 33)
//reducer : 20, 0 (tid = 33)
//transformer : 30 (tid = 35)
//reducer : 0, 30 (tid = 35)
//reducer : 0, 30 (tid = 35)
//reducer : 40, 10 (tid = 35)
//reducer : 50, 30 (tid = 35)
//reducer : 0, 50 (tid = 35)
//reducer : 50, 20 (tid = 35)
//reducer : 70, 80 (tid = 35)
//----
//ret = 150
double reduceToDouble (long parallelismThreshold, ToDoubleBiFunction<? super K,? super V> transformer, double basis, DoubleBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Double>();
map.put("a", 10.0);
map.put("b", 20.0);
map.put("c", 30.0);
map.put("d", 40.0);
map.put("e", 50.0);
System.out.println(map); // {a=10.0, b=20.0, c=30.0, d=40.0, e=50.0}
System.out.println("-- reduce --");
final var ret = map.reduceToDouble(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s = %s (tid = %d)%n", key, value, tid);
return value;
}, 0.0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %f, %f (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a = 10.0 (tid = 1)
//reducer : 0.000000, 10.000000 (tid = 1)
//transformer : b = 20.0 (tid = 34)
//reducer : 0.000000, 20.000000 (tid = 34)
//transformer : c = 30.0 (tid = 34)
//reducer : 20.000000, 30.000000 (tid = 34)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//transformer : d = 40.0 (tid = 33)
//reducer : 0.000000, 40.000000 (tid = 33)
//transformer : e = 50.0 (tid = 33)
//reducer : 40.000000, 50.000000 (tid = 33)
//reducer : 90.000000, 0.000000 (tid = 33)
//reducer : 10.000000, 50.000000 (tid = 33)
//reducer : 60.000000, 90.000000 (tid = 33)
//reducer : 150.000000, 0.000000 (tid = 33)
//----
//ret = 150.0
int reduceToInt (long parallelismThreshold, ToIntBiFunction<? super K,? super V> transformer, int basis, IntBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceToInt(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s = %d (tid = %d)%n", key, value, tid);
return value;
}, 0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a = 10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//transformer : b = 20 (tid = 1)
//reducer : 0, 20 (tid = 1)
//transformer : c = 30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//transformer : d = 40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//transformer : e = 50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
long reduceToLong (long parallelismThreshold, ToLongBiFunction<? super K,? super V> transformer, long basis, LongBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Long>();
map.put("a", 10L);
map.put("b", 20L);
map.put("c", 30L);
map.put("d", 40L);
map.put("e", 50L);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceToLong(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s = %d (tid = %d)%n", key, value, tid);
return value;
}, 0L, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : a = 10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//transformer : b = 20 (tid = 1)
//reducer : 0, 20 (tid = 1)
//transformer : c = 30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//transformer : d = 40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//transformer : e = 50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
V reduceValues (long parallelismThreshold, BiFunction<? super V,? super V,? extends V> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceValues(1, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//reducer : 20, 30 (tid = 1)
//reducer : 40, 50 (tid = 1)
//reducer : 10, 50 (tid = 32)
//reducer : 60, 90 (tid = 32)
//----
//ret = 150
<U> U reduceValues (long parallelismThreshold, Function<? super V,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceValues(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %d (tid = %d)%n", value, tid);
return value;
}, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %s, %s (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 10 (tid = 1)
//transformer : 20 (tid = 1)
//transformer : 30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//transformer : 40 (tid = 33)
//transformer : 50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//----
//ret = 150
double reduceValuesToDouble (long parallelismThreshold, ToDoubleFunction<? super V> transformer, double basis, DoubleBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Double>();
map.put("a", 10.0);
map.put("b", 20.0);
map.put("c", 30.0);
map.put("d", 40.0);
map.put("e", 50.0);
System.out.println(map); // {a=10.0, b=20.0, c=30.0, d=40.0, e=50.0}
System.out.println("-- reduce --");
final var ret = map.reduceValuesToDouble(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %s (tid = %d)%n", value, tid);
return value;
}, 0.0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %f, %f (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 10.0 (tid = 1)
//reducer : 0.000000, 10.000000 (tid = 1)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//reducer : 0.000000, 0.000000 (tid = 32)
//transformer : 40.0 (tid = 33)
//reducer : 0.000000, 40.000000 (tid = 33)
//transformer : 50.0 (tid = 33)
//reducer : 40.000000, 50.000000 (tid = 33)
//reducer : 90.000000, 0.000000 (tid = 33)
//transformer : 20.0 (tid = 35)
//reducer : 0.000000, 20.000000 (tid = 35)
//transformer : 30.0 (tid = 35)
//reducer : 20.000000, 30.000000 (tid = 35)
//reducer : 10.000000, 50.000000 (tid = 35)
//reducer : 60.000000, 90.000000 (tid = 35)
//reducer : 150.000000, 0.000000 (tid = 35)
//----
//ret = 150.0
int reduceValuesToInt (long parallelismThreshold, ToIntFunction<? super V> transformer, int basis, IntBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceValuesToInt(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %d (tid = %d)%n", value, tid);
return value;
}, 0, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//transformer : 20 (tid = 1)
//reducer : 0, 20 (tid = 1)
//transformer : 30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//reducer : 0, 0 (tid = 34)
//reducer : 0, 0 (tid = 34)
//reducer : 0, 0 (tid = 34)
//transformer : 40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//transformer : 50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
long reduceValuesToLong (long parallelismThreshold, ToLongFunction<? super V> transformer, long basis, LongBinaryOperator reducer)
final var map = new ConcurrentHashMap<String, Long>();
map.put("a", 10L);
map.put("b", 20L);
map.put("c", 30L);
map.put("d", 40L);
map.put("e", 50L);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
System.out.println("-- reduce --");
final var ret = map.reduceValuesToLong(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("transformer : %d (tid = %d)%n", value, tid);
return value;
}, 0L, (v1, v2) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("reducer : %d, %d (tid = %d)%n", v1, v2, tid);
return v1 + v2;
});
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//-- reduce --
//transformer : 10 (tid = 1)
//reducer : 0, 10 (tid = 1)
//transformer : 20 (tid = 1)
//reducer : 0, 20 (tid = 1)
//transformer : 30 (tid = 1)
//reducer : 20, 30 (tid = 1)
//transformer : 40 (tid = 33)
//reducer : 0, 40 (tid = 33)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//reducer : 0, 0 (tid = 32)
//transformer : 50 (tid = 33)
//reducer : 40, 50 (tid = 33)
//reducer : 90, 0 (tid = 33)
//reducer : 10, 50 (tid = 33)
//reducer : 60, 90 (tid = 33)
//reducer : 150, 0 (tid = 33)
//----
//ret = 150
V remove (Object key)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.remove("b")); // 2
System.out.println(map); // {a=1, c=3}
System.out.println(map.remove("z")); // null
System.out.println(map); // {a=1, c=3}
boolean remove (Object key, Object value)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.remove("a", 1)); // true
System.out.println(map); // {b=2, c=3}
System.out.println(map.remove("b", 1)); // false
System.out.println(map); // {b=2, c=3}
System.out.println(map.remove("b", 2)); // true
System.out.println(map); // {c=3}
System.out.println(map.remove("C", 3)); // false
System.out.println(map); // {c=3}
System.out.println(map.remove("d", 4)); // false
System.out.println(map); // {c=3}
V replace (K key, V value)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.replace("a", 10)); // 1
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 20)); // 2
System.out.println(map); // {a=10, b=20, c=3}
System.out.println(map.replace("z", 99)); // null
System.out.println(map); // {a=10, b=20, c=3}
boolean replace (K key, V oldValue, V newValue)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.replace("a", 1, 10)); // true
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 999, 20)); // false
System.out.println(map); // {a=10, b=2, c=3}
System.out.println(map.replace("b", 2, 20)); // true
System.out.println(map); // {a=10, b=20, c=3}
System.out.println(map.replace("z", 3, 999)); // false
System.out.println(map); // {a=10, b=20, c=3}
<U> U search (long parallelismThreshold, BiFunction<? super K,? super V,? extends U> searchFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
final var ret = map.search(1, (key, value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s = %d (tid = %d)%n", key, value, tid);
return "c".equals(key) ? "Hit! (%s, %d)".formatted(key, value) : null;
});
TimeUnit.SECONDS.sleep(1);
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//a = 10 (tid = 1)
//b = 20 (tid = 1)
//c = 30 (tid = 1)
//d = 40 (tid = 32)
//----
//ret = Hit! (c, 30)
<U> U searchEntries (long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> searchFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
final var ret = map.searchEntries(1, (entry) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s (tid = %d)%n", entry, tid);
return "c".equals(entry.getKey()) ? "Hit! (%s)".formatted(entry) : null;
});
TimeUnit.SECONDS.sleep(1);
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//a=10 (tid = 1)
//b=20 (tid = 1)
//c=30 (tid = 1)
//d=40 (tid = 32)
//----
//ret = Hit! (c=30)
<U> U searchKeys (long parallelismThreshold, Function<? super K,? extends U> searchFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
final var ret = map.searchKeys(1, (key) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%s (tid = %d)%n", key, tid);
return "c".equals(key) ? "Hit! (%s)".formatted(key) : null;
});
TimeUnit.SECONDS.sleep(1);
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//a (tid = 1)
//b (tid = 1)
//c (tid = 1)
//d (tid = 32)
//----
//ret = Hit! (c)
<U> U searchValues (long parallelismThreshold, Function<? super V,? extends U> searchFunction)
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
map.put("d", 40);
map.put("e", 50);
System.out.println(map); // {a=10, b=20, c=30, d=40, e=50}
final var ret = map.searchValues(1, (value) -> {
final var tid = Thread.currentThread().threadId();
System.out.printf("%d (tid = %d)%n", value, tid);
return 30 == value ? "Hit! (%d)".formatted(value) : null;
});
TimeUnit.SECONDS.sleep(1);
System.out.println("----");
System.out.println("ret = " + ret);
// 結果
// ↓
//10 (tid = 1)
//20 (tid = 1)
//30 (tid = 1)
//40 (tid = 32)
//----
//ret = Hit! (30)
int size ()
size() の代わりに mappingCount() を使うことが推奨されています。
final var map = new ConcurrentHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.size()); // 0
map.put("a", 1);
System.out.println(map); // {a=1}
System.out.println(map.size()); // 1
map.put("b", 2);
System.out.println(map); // {a=1, b=2}
System.out.println(map.size()); // 2
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3
String toString ()
final var map = new ConcurrentHashMap<String, Integer>();
final var str1 = map.toString();
System.out.println(str1); // {}
map.put("a", 1);
final var str2 = map.toString();
System.out.println(str2); // {a=1}
map.put("b", 2);
final var str3 = map.toString();
System.out.println(str3); // {a=1, b=2}
map.put("c", 3);
final var str4 = map.toString();
System.out.println(str4); // {a=1, b=2, c=3}
Collection<V> values ()
final var map = new ConcurrentHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}
final var values = map.values();
System.out.println(values); // [1, 2, 3]
map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]
values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]
//values.add(4); // UnsupportedOperationException
AbstractMapで宣言されたメソッド
clone, isEmpty, size
「Java API 使用例 : AbstractMap」をご参照ください。
ConcurrentMapで宣言されたメソッド
forEach, replaceAll
「Java API 使用例 : ConcurrentMap」をご参照ください。
関連記事
- API 使用例
- BlockingQueue (ブロッキング・キュー)
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView (並列処理用セット)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- ConcurrentMap (並列処理用マップ)
- ConcurrentModificationException (並列処理例外)
- ConcurrentSkipListSet (並列処理用セット)
- Condition (同期)
- CopyOnWriteArrayList (並列処理用リスト)
- CopyOnWriteArraySet (並列処理用セット)
- CountDownLatch (同期)
- CyclicBarrier (同期)
- Exchanger (同期)
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException (割込み例外)
- Lock (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit