Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
How to modify the list in ConcurrentHashMap? - bug-code.com Q&A

How to modify the list in ConcurrentHashMap?

0 like 0 dislike
26 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 | 26 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
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers

0 comments

12.3k users

24,788 questions
46,220 answers
0 comments
12,337 users