-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentMapDemo.java
More file actions
46 lines (33 loc) · 1.41 KB
/
ConcurrentMapDemo.java
File metadata and controls
46 lines (33 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ConcurrentMapDemo {
public static void main(String[] args) {
ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
map.put("foo", "bar");
map.put("han", "solo");
map.put("r2", "d2");
map.put("c3", "p0");
/*
* The iteration is performed sequentially on the current thread.
*/
map.forEach((key, value) -> System.out.printf("%s = %s\n", key, value));
String value = map.putIfAbsent("c3", "p1");
System.out.println(value); // p0
value = map.putIfAbsent("c4", "p1");
System.out.println(map.get("c4")); // p1
value = map.getOrDefault("hi", "there");
System.out.println(value); // there
value = map.getOrDefault("c4", "there");
System.out.println(value); // p1
map.replaceAll((key, v) -> "r2".equals(key) ? "d3" : v+"9");
System.out.println(map.get("r2")); // d3
map.forEach((key, v) -> System.out.printf("%s = %s\n", key, v));
//transfer key foo's value
map.compute("foo", (key, v) -> v.substring(0, v.length()-1) + v.substring(0, v.length()-1));
System.out.println(map.get("foo")); // barbar
map.forEach((key, v) -> System.out.printf("%s = %s\n", key, v));
//change foo key's value to boo + "was " + barbar
map.merge("foo", "boo", (oldVal, newVal) -> newVal + " was " + oldVal);
System.out.println(map.get("foo")); // boo was barbar
}
}