How to modify the list in ConcurrentHashMap?

0 like 0 dislike
24 views
The value in ConcurrentHashMap is a list - List.
You need to modify is to add another value. When you try to make a Map.get(i).add(value) error occurs java.lang.An UnsupportedOperationException. I understand that that this list to modify a thread-safe way. but how to do not quite understand.
Please help.
asked by | 24 views

2 Answers

0 like 0 dislike
import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function;

...
public final Map> map = new ConcurrentHashMap<>(); public final Function> mapFun = i -> new CopyOnWriteArrayList<>();

...
map.computeIfAbsent(1, mapFun).add("a"); map.computeIfAbsent(1, mapFun).add("b"); map.computeIfAbsent(1, mapFun).add("c");
answered by
0 like 0 dislike
The List interface has an add operation.
To replenish the list, it should be ArrayList, LinkedList , Vector. https://en.wikiversity.org/wiki/Java_Collections_O...

\rhttps://docs.oracle.com/javase/8/docs/api/java/uti...
Appends the specified element to the end of this list (optional operation).
answered by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
24,509 questions
45,932 answers
0 comments
345 users