A ConcurrentSkipListMap method replaceAll() replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
1 default void replaceAll(BiFunction<? super K,? super V,? extends V> function)
function : It is a function to apply to each entry.
ConcurrentSkipListMap method replaceAll()
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9 ConcurrentSkipListMap<Integer, Integer> listMap = new ConcurrentSkipListMap<>();
10 listMap.put(11, 111);
11 listMap.put(22, 222);
12 listMap.put(33, 333);
13 System.out.println("Map elements : " + listMap);
14
15
16 listMap.replaceAll((k, v) -> k > v ? v : v * 2);
17 System.out.println("Map elements : " + listMap);
18 }
19 }
In the above example, a ConcurrentSkipListMap object with key and value of type Integer and mappings added using put() method. A replaceAll() method called by passing a BiFunction that validates whether key greater than value, it returns value otherwise multiply value with 2 and reassign to the value to the respective key. An updated map object print that includes mapped values.
1 Map elements : {11=111, 22=222, 33=333}
2 Map elements : {11=222, 22=444, 33=666}
ConcurrentSkipListMap method replaceAll() with String
ConcurrentSkipListMap method replaceAll() with String
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class StringExp {
6
7 public static void main(String[] args) {
8
9 ConcurrentSkipListMap<String, String> listMap = new ConcurrentSkipListMap<>();
10 listMap.put("A1", "Welcome");
11 listMap.put("A2", "to");
12 listMap.put("A3", "IOGyan");
13 System.out.println("Map elements : " + listMap);
14
15
16 listMap.replaceAll((k, v) -> v.length() > 5 ? v.toUpperCase() : v);
17 System.out.println("Map elements : " + listMap);
18 }
19 }
In the above example, a ConcurrentSkipListMap object with key and value of type String and mappings added using put() method. A replaceAll() method called by passing a BiFunction that validates whether string length greater than 5, it converts mapped string to uppercase otherwise returns original string and reassign to the value to the respective key. An updated map mappings printed that includes updated mapped values.
1 Map elements : {A1=Welcome, A2=to, A3=IOGyan}
2 Map elements : {A1=WELCOME, A2=to, A3=IOGYAN}
ConcurrentSkipListMap method replaceAll() with custom object
ConcurrentSkipListMap method replaceAll() with custom object
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class Example {
6
7 public static void main(String[] args) {
8
9 Product pd1 = new Product(11, "Phone", 10, 23000.00);
10 Product pd2 = new Product(22, "LCD", 2, 90000.32);
11 Product pd3 = new Product(33, "TV", 5, 500000.94);
12
13 ConcurrentSkipListMap<Product, String> listMap = new ConcurrentSkipListMap<>();
14 listMap.put(pd1, "Welcome");
15 listMap.put(pd2, "to");
16 System.out.println("Map elements : " + listMap);
17
18
19 listMap.replaceAll((k, v) -> k.getPrice() > 25000 ? v.toUpperCase() : v);
20 System.out.println("Map elements : " + listMap);
21 }
22 }
In the above example, a ConcurrentSkipListMap with key and value of type Product and String and entries added using put() method. A replaceAll() method called by passing a BiFunction that validates whether product price greater than 25000, it converts mapped string to uppercase otherwise returns original string and reassign to the value to the respective key. An updated map entries printed that includes updated mapped value.
1 Map elements : {Phone 10 23000.0=Welcome, LCD 2 90000.32=to}
2 Map elements : {Phone 10 23000.0=Welcome, LCD 2 90000.32=TO}
Product class
1 package com.java;
2
3 public class Product implements Comparable<Product> {
4 private final int pid;
5 private final String name;
6 private final long quantity;
7
8 private final double price;
9
10 public Product(int pid, String name, long quantity, double price) {
11 this.pid = pid;
12 this.name = name;
13 this.quantity = quantity;
14 this.price = price;
15 }
16
17 public int getPid() {
18 return pid;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public long getQuantity() {
26 return quantity;
27 }
28
29 public double getPrice() {
30 return price;
31 }
32
33 @Override
34 public String toString() {
35 return this.name + " " + this.quantity + " " + this.price;
36 }
37
38 @Override
39 public int compareTo(Product o) {
40 return this.pid - o.pid;
41 }
42 }
Related options for your search