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

How to modify the list in ConcurrentHashMap?

0 like 0 dislike
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.

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


12.3k users

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