Java : ConcurrentSkipListMap with Examples
ConcurrentSkipListMap (Java SE 20 & JDK 20) API Examples.
You will find code examples on most ConcurrentSkipListMap methods.
Summary
A scalable concurrent ConcurrentNavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
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<>());
// Result
// ↓
//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<>());
// Result
// ↓
//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);
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);
System.out.println(hashMap); // {aaa=30, ccc=10, bbb=40, ddd=20}
Constructors
ConcurrentSkipListMap ()
Constructs a new, empty map, sorted according to the natural ordering of the keys.
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)
Constructs a new, empty map, sorted according to the specified 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)
Constructs a new map containing the same mappings as the given map, sorted according to the natural ordering of the keys.
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)
Constructs a new map containing the same mappings and using the same ordering as the specified sorted map.
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
Methods
Map.Entry<K,V> ceilingEntry (K key)
Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such entry.
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)
Returns the least key greater than or equal to the given key, or null if there is no such 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 ()
Removes all of the mappings from this map.
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 ()
Returns a shallow copy of this ConcurrentSkipListMap instance.
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 ()
Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
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)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
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)
If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.
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}
final var map = new ConcurrentSkipListMap<String, List<String>>();
System.out.println(map); // {}
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)
If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped 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}
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)
Returns true if this map contains a mapping for the specified 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)
Returns true if this map maps one or more keys to the specified 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 ()
Returns a reverse order NavigableSet view of the keys contained in this map.
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 ()
Returns a reverse order view of the mappings contained in this map.
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 ()
Returns a Set view of the mappings contained in this map.
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)
Compares the specified object with this map for equality.
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 ()
Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
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 ()
Returns the first (lowest) key currently in this map.
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!");
}
// Result
// ↓
//NoSuchElementException!
Map.Entry<K,V> floorEntry (K key)
Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such 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)
Returns the greatest key less than or equal to the given key, or null if there is no such 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)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the 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)
Returns the value to which the specified key is mapped, or the given defaultValue if this map contains no mapping for the 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.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)
Returns a view of the portion of this map whose keys are strictly less than 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)
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) 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("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)
Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such 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)
Returns the least key strictly greater than the given key, or null if there is no such 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 ()
Returns true if this map contains no key-value mappings.
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 ()
Returns a NavigableSet view of the keys contained in this map.
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 ()
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
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 ()
Returns the last (highest) key currently in this map.
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!");
}
// Result
// ↓
//NoSuchElementException!
Map.Entry<K,V> lowerEntry (K key)
Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such 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)
Returns the greatest key strictly less than the given key, or null if there is no such 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)
If the specified key is not already associated with a value, associates it with the given 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}
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 ()
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
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 ()
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
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)
Associates the specified value with the specified key in this map.
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)
If the specified key is not already associated with a value, associates it with the given 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)
Removes the mapping for the specified key from this map if present.
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)
Removes the entry for a key only if currently mapped to a given 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)
Replaces the entry for a key only if currently mapped to some 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)
Replaces the entry for a key only if currently mapped to a given 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", 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 ()
Returns the number of key-value mappings in this map.
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)
Returns a view of the portion of this map whose keys range from fromKey to 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", 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)
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
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)
Returns a view of the portion of this map whose keys are greater than or equal to 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)
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) 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("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 ()
Returns a Collection view of the values contained in this map.
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
Methods declared in AbstractMap
hashCode, isEmpty, putAll, size, toString
Please see the link below.
Methods declared in ConcurrentMap
Methods declared in Map
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- ExecutionException
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit