Java : ConcurrentSkipListMap (並列処理用マップ) - API使用例
ConcurrentSkipListMap (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ConcurrentSkipListMap クラスは、反復処理 (例えば for-eachループ文) 中に put や remove などによる変更を許容する Map の実装です。
非チェック例外の ConcurrentModificationException が発生しないことが保証されています。
汎用コレクションの TreeMap に相当します。
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 TreeMap<>());
// 結果
// ↓
//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 ConcurrentSkipListMap<>());
// 結果
// ↓
//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}
final var navigableMap = new ConcurrentSkipListMap<String, Integer>();
navigableMap.put("ccc", 10);
navigableMap.put("ddd", 20);
navigableMap.put("aaa", 30);
navigableMap.put("bbb", 40);
// NavigableMap : 要素の順番は、キーの自然順序でソートされます。
System.out.println(navigableMap); // {aaa=30, bbb=40, ccc=10, ddd=20}
final var hashMap = new ConcurrentHashMap<String, Integer>();
hashMap.put("ccc", 10);
hashMap.put("ddd", 20);
hashMap.put("aaa", 30);
hashMap.put("bbb", 40);
// HashMap : 要素の順番は保持されません。
System.out.println(hashMap); // {aaa=30, ccc=10, bbb=40, ddd=20}
コンストラクタ
ConcurrentSkipListMap ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.isEmpty()); // true
map.put("b", 10);
map.put("a", 20);
map.put("c", 30);
System.out.println(map); // {a=20, b=10, c=30}
System.out.println(map.size()); // 3
final var map = new ConcurrentSkipListMap<Integer, String>();
map.put(100, "a");
map.put(999, "b");
map.put(-200, "c");
System.out.println(map); // {-200=c, 100=a, 999=b}
ConcurrentSkipListMap (Comparator<? super K> comparator)
final var src = Map.of("bbb", 10, "aaa", 20, "ccc", 30);
final var map1 = new ConcurrentSkipListMap<String, Integer>();
map1.putAll(src);
System.out.println(map1); // {aaa=20, bbb=10, ccc=30}
final var map2 = new ConcurrentSkipListMap<String, Integer>(Comparator.reverseOrder());
map2.putAll(src);
System.out.println(map2); // {ccc=30, bbb=10, aaa=20}
ConcurrentSkipListMap (Map<? extends K,? extends V> m)
final var m = Map.of("b", 10, "a", 20, "c", 30);
final var map = new ConcurrentSkipListMap<>(m);
System.out.println(map); // {a=20, b=10, c=30}
System.out.println(map.size()); // 3
ConcurrentSkipListMap (SortedMap<K,? extends V> m)
final var m = new ConcurrentSkipListMap<String, Integer>(Comparator.reverseOrder());
m.put("a", 10);
m.put("b", 20);
m.put("c", 30);
System.out.println(m); // {c=30, b=20, a=10}
final var map = new ConcurrentSkipListMap<>(m);
System.out.println(map); // {c=30, b=20, a=10}
System.out.println(map.size()); // 3
メソッド
Map.Entry<K,V> ceilingEntry (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.ceilingEntry("a")); // b=10
System.out.println(map.ceilingEntry("b")); // b=10
System.out.println(map.ceilingEntry("c")); // d=20
System.out.println(map.ceilingEntry("d")); // d=20
System.out.println(map.ceilingEntry("e")); // f=30
System.out.println(map.ceilingEntry("f")); // f=30
System.out.println(map.ceilingEntry("g")); // null
K ceilingKey (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.ceilingKey("a")); // b
System.out.println(map.ceilingKey("b")); // b
System.out.println(map.ceilingKey("c")); // d
System.out.println(map.ceilingKey("d")); // d
System.out.println(map.ceilingKey("e")); // f
System.out.println(map.ceilingKey("f")); // f
System.out.println(map.ceilingKey("g")); // null
void clear ()
final var map = new ConcurrentSkipListMap<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
ConcurrentSkipListMap<K,V> clone ()
final var map = new ConcurrentSkipListMap<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 cloned = map.clone();
System.out.println(cloned); // {a=1, b=2, c=3}
System.out.println(cloned.getClass()); // class java.util.concurrent.ConcurrentSkipListMap
Comparator<? super K> comparator ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
final var comparator = map.comparator();
System.out.println(comparator); // null
final var map = new ConcurrentSkipListMap<String, Integer>(Comparator.reverseOrder());
System.out.println(map); // {}
final var comparator = map.comparator();
System.out.println(comparator.equals(Comparator.reverseOrder())); // true
final var map = new ConcurrentSkipListMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
System.out.println(map); // {}
final var comparator = map.comparator();
System.out.println(comparator.equals(String.CASE_INSENSITIVE_ORDER)); // true
V compute (K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
final var map = new ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 containsKey (Object key)
final var map = new ConcurrentSkipListMap<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 ConcurrentSkipListMap<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
NavigableSet<K> descendingKeySet ()
final var map = new ConcurrentSkipListMap<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 descKeys = map.descendingKeySet();
System.out.println(descKeys); // [c, b, a]
map.put("d", 40);
System.out.println(map); // {a=10, b=20, c=30, d=40}
System.out.println(descKeys); // [d, c, b, a]
descKeys.remove("b");
System.out.println(map); // {a=10, c=30, d=40}
System.out.println(descKeys); // [d, c, a]
ConcurrentNavigableMap<K,V> descendingMap ()
final var map = new ConcurrentSkipListMap<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 descMap = map.descendingMap();
System.out.println(descMap); // {c=30, b=20, a=10}
map.put("d", 40);
System.out.println(map); // {a=10, b=20, c=30, d=40}
System.out.println(descMap); // {d=40, c=30, b=20, a=10}
descMap.remove("b");
System.out.println(map); // {a=10, c=30, d=40}
System.out.println(descMap); // {d=40, c=30, a=10}
Set<Map.Entry<K,V>> entrySet ()
final var map = new ConcurrentSkipListMap<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]
boolean equals (Object o)
final var map1 = new ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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
Map.Entry<K,V> firstEntry ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.firstEntry()); // null
map.put("b", 20);
System.out.println(map); // {b=20}
System.out.println(map.firstEntry()); // b=20
map.put("c", 30);
System.out.println(map); // {b=20, c=30}
System.out.println(map.firstEntry()); // b=20
map.put("a", 10);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.firstEntry()); // a=10
K firstKey ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
map.put("b", 20);
System.out.println(map); // {b=20}
System.out.println(map.firstKey()); // b
map.put("c", 30);
System.out.println(map); // {b=20, c=30}
System.out.println(map.firstKey()); // b
map.put("a", 10);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.firstKey()); // a
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
try {
map.firstKey();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
Map.Entry<K,V> floorEntry (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.floorEntry("a")); // null
System.out.println(map.floorEntry("b")); // b=10
System.out.println(map.floorEntry("c")); // b=10
System.out.println(map.floorEntry("d")); // d=20
System.out.println(map.floorEntry("e")); // d=20
System.out.println(map.floorEntry("f")); // f=30
System.out.println(map.floorEntry("g")); // f=30
K floorKey (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.floorKey("a")); // null
System.out.println(map.floorKey("b")); // b
System.out.println(map.floorKey("c")); // b
System.out.println(map.floorKey("d")); // d
System.out.println(map.floorKey("e")); // d
System.out.println(map.floorKey("f")); // f
System.out.println(map.floorKey("g")); // f
V get (Object key)
final var map = new ConcurrentSkipListMap<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 ConcurrentSkipListMap<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
ConcurrentNavigableMap<K,V> headMap (K toKey)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);
System.out.println(map); // {x=10, y=20, z=30}
final var headMap = map.headMap("z");
System.out.println(headMap); // {x=10, y=20}
map.put("a", 40);
System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(headMap); // {a=40, x=10, y=20}
headMap.remove("x");
System.out.println(map); // {a=40, y=20, z=30}
System.out.println(headMap); // {a=40, y=20}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.headMap("a")); // {}
System.out.println(map.headMap("b")); // {a=10}
System.out.println(map.headMap("c")); // {a=10, b=20}
System.out.println(map.headMap("d")); // {a=10, b=20, c=30}
ConcurrentNavigableMap<K,V> headMap (K toKey, boolean inclusive)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);
System.out.println(map); // {x=10, y=20, z=30}
final var headMap = map.headMap("y", true);
System.out.println(headMap); // {x=10, y=20}
map.put("a", 40);
System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(headMap); // {a=40, x=10, y=20}
headMap.remove("x");
System.out.println(map); // {a=40, y=20, z=30}
System.out.println(headMap); // {a=40, y=20}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.headMap("a")); // {}
System.out.println(map.headMap("a", true)); // {a=10}
System.out.println(map.headMap("b")); // {a=10}
System.out.println(map.headMap("b", true)); // {a=10, b=20}
System.out.println(map.headMap("c")); // {a=10, b=20}
System.out.println(map.headMap("c", true)); // {a=10, b=20, c=30}
System.out.println(map.headMap("d")); // {a=10, b=20, c=30}
System.out.println(map.headMap("d", true)); // {a=10, b=20, c=30}
Map.Entry<K,V> higherEntry (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.higherEntry("a")); // b=10
System.out.println(map.higherEntry("b")); // d=20
System.out.println(map.higherEntry("c")); // d=20
System.out.println(map.higherEntry("d")); // f=30
System.out.println(map.higherEntry("e")); // f=30
System.out.println(map.higherEntry("f")); // null
System.out.println(map.higherEntry("g")); // null
K higherKey (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.higherKey("a")); // b
System.out.println(map.higherKey("b")); // d
System.out.println(map.higherKey("c")); // d
System.out.println(map.higherKey("d")); // f
System.out.println(map.higherKey("e")); // f
System.out.println(map.higherKey("f")); // null
System.out.println(map.higherKey("g")); // null
boolean isEmpty ()
final var map = new ConcurrentSkipListMap<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
NavigableSet<K> keySet ()
final var map = new ConcurrentSkipListMap<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]
Map.Entry<K,V> lastEntry ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.lastEntry()); // null
map.put("b", 20);
System.out.println(map); // {b=20}
System.out.println(map.lastEntry()); // b=20
map.put("a", 10);
System.out.println(map); // {a=10, b=20}
System.out.println(map.lastEntry()); // b=20
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.lastEntry()); // c=30
K lastKey ()
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
map.put("b", 20);
System.out.println(map); // {b=20}
System.out.println(map.lastKey()); // b
map.put("a", 10);
System.out.println(map); // {a=10, b=20}
System.out.println(map.lastKey()); // b
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.lastKey()); // c
final var map = new ConcurrentSkipListMap<String, Integer>();
System.out.println(map); // {}
try {
map.lastKey();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
Map.Entry<K,V> lowerEntry (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.lowerEntry("a")); // null
System.out.println(map.lowerEntry("b")); // null
System.out.println(map.lowerEntry("c")); // b=10
System.out.println(map.lowerEntry("d")); // b=10
System.out.println(map.lowerEntry("e")); // d=20
System.out.println(map.lowerEntry("f")); // d=20
System.out.println(map.lowerEntry("g")); // f=30
K lowerKey (K key)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("b", 10);
map.put("d", 20);
map.put("f", 30);
System.out.println(map); // {b=10, d=20, f=30}
System.out.println(map.lowerKey("a")); // null
System.out.println(map.lowerKey("b")); // null
System.out.println(map.lowerKey("c")); // b
System.out.println(map.lowerKey("d")); // b
System.out.println(map.lowerKey("e")); // d
System.out.println(map.lowerKey("f")); // d
System.out.println(map.lowerKey("g")); // f
V merge (K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
final var map = new ConcurrentSkipListMap<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}
Map.Entry<K,V> pollFirstEntry ()
final var map = new ConcurrentSkipListMap<String, Integer>();
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.pollFirstEntry()); // a=10
System.out.println(map); // {b=20, c=30}
System.out.println(map.pollFirstEntry()); // b=20
System.out.println(map); // {c=30}
System.out.println(map.pollFirstEntry()); // c=30
System.out.println(map); // {}
System.out.println(map.pollFirstEntry()); // null
Map.Entry<K,V> pollLastEntry ()
final var map = new ConcurrentSkipListMap<String, Integer>();
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.pollLastEntry()); // c=30
System.out.println(map); // {a=10, b=20}
System.out.println(map.pollLastEntry()); // b=20
System.out.println(map); // {a=10}
System.out.println(map.pollLastEntry()); // a=10
System.out.println(map); // {}
System.out.println(map.pollLastEntry()); // null
V put (K key, V value)
final var map = new ConcurrentSkipListMap<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}
V putIfAbsent (K key, V value)
final var map = new ConcurrentSkipListMap<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}
V remove (Object key)
final var map = new ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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 ConcurrentSkipListMap<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}
int size ()
final var map = new ConcurrentSkipListMap<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
ConcurrentNavigableMap<K,V> subMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);
System.out.println(map); // {x=10, y=20, z=30}
final var subMap = map.subMap("a", true, "y", true);
System.out.println(subMap); // {x=10, y=20}
map.put("a", 40);
System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(subMap); // {a=40, x=10, y=20}
subMap.remove("x");
System.out.println(map); // {a=40, y=20, z=30}
System.out.println(subMap); // {a=40, y=20}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.subMap("a", true, "c", true)); // {a=10, b=20, c=30}
System.out.println(map.subMap("a", true, "c", false)); // {a=10, b=20}
System.out.println(map.subMap("a", false, "c", true)); // {b=20, c=30}
System.out.println(map.subMap("a", false, "c", false)); // {b=20}
System.out.println(map.subMap("a", true, "a", false)); // {}
System.out.println(map.subMap("a", true, "b", false)); // {a=10}
System.out.println(map.subMap("a", true, "c", false)); // {a=10, b=20}
System.out.println(map.subMap("a", true, "d", false)); // {a=10, b=20, c=30}
System.out.println(map.subMap("a", true, "d", false)); // {a=10, b=20, c=30}
System.out.println(map.subMap("b", true, "d", false)); // {b=20, c=30}
System.out.println(map.subMap("c", true, "d", false)); // {c=30}
System.out.println(map.subMap("d", true, "d", false)); // {}
ConcurrentNavigableMap<K,V> subMap (K fromKey, K toKey)
final var map = new ConcurrentSkipListMap<String, Integer>();
map.put("x", 10);
map.put("y", 20);
map.put("z", 30);
System.out.println(map); // {x=10, y=20, z=30}
final var subMap = map.subMap("a", "z");
System.out.println(subMap); // {x=10, y=20}
map.put("a", 40);
System.out.println(map); // {a=40, x=10, y=20, z=30}
System.out.println(subMap); // {a=40, x=10, y=20}
subMap.remove("x");
System.out.println(map); // {a=40, y=20, z=30}
System.out.println(subMap); // {a=40, y=20}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.subMap("a", "a")); // {}
System.out.println(map.subMap("a", "b")); // {a=10}
System.out.println(map.subMap("a", "c")); // {a=10, b=20}
System.out.println(map.subMap("a", "d")); // {a=10, b=20, c=30}
System.out.println(map.subMap("a", "d")); // {a=10, b=20, c=30}
System.out.println(map.subMap("b", "d")); // {b=20, c=30}
System.out.println(map.subMap("c", "d")); // {c=30}
System.out.println(map.subMap("d", "d")); // {}
ConcurrentNavigableMap<K,V> tailMap (K fromKey)
final var map = new ConcurrentSkipListMap<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 tailMap = map.tailMap("b");
System.out.println(tailMap); // {b=20, c=30}
map.put("d", 40);
System.out.println(map); // {a=10, b=20, c=30, d=40}
System.out.println(tailMap); // {b=20, c=30, d=40}
tailMap.remove("b");
System.out.println(map); // {a=10, c=30, d=40}
System.out.println(tailMap); // {c=30, d=40}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.tailMap("a")); // {a=10, b=20, c=30}
System.out.println(map.tailMap("b")); // {b=20, c=30}
System.out.println(map.tailMap("c")); // {c=30}
System.out.println(map.tailMap("d")); // {}
ConcurrentNavigableMap<K,V> tailMap (K fromKey, boolean inclusive)
final var map = new ConcurrentSkipListMap<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 tailMap = map.tailMap("a", false);
System.out.println(tailMap); // {b=20, c=30}
map.put("d", 40);
System.out.println(map); // {a=10, b=20, c=30, d=40}
System.out.println(tailMap); // {b=20, c=30, d=40}
tailMap.remove("b");
System.out.println(map); // {a=10, c=30, d=40}
System.out.println(tailMap); // {c=30, d=40}
final var map = new ConcurrentSkipListMap<String, Integer>();
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.tailMap("a")); // {a=10, b=20, c=30}
System.out.println(map.tailMap("a", false)); // {b=20, c=30}
System.out.println(map.tailMap("b")); // {b=20, c=30}
System.out.println(map.tailMap("b", false)); // {c=30}
System.out.println(map.tailMap("c")); // {c=30}
System.out.println(map.tailMap("c", false)); // {}
Collection<V> values ()
final var map = new ConcurrentSkipListMap<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で宣言されたメソッド
hashCode, isEmpty, putAll, size, toString
「Java API 使用例 : AbstractMap」をご参照ください。
ConcurrentMapで宣言されたメソッド
forEach, replaceAll
「Java API 使用例 : ConcurrentMap」をご参照ください。
Mapで宣言されたメソッド
hashCode, putAll
「Java API 使用例 : Map」をご参照ください。
関連記事
- 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