I am trying to understand the merge method. I have a map of <Character, List<Integer>>
. I want to add the indices to the list of the appropriate key. I want to use the merge method to do so. I tried the following method. I am getting UnsupportedOperationException
. What is the best way to do so using the merge
method?
Map<Character, List<Integer>> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
int t = i;
map.merge(s.charAt(i), Arrays.asList(t), (c, list) -> {
map.get(s.charAt(t)).add(t);
return c;
});
}