0

I have this kind of map:

Map<String, List<String>> map = new HashMap<>();

And I'm trying to edit one of my list's values. This is how I tried to achieve that:

map.get(key).toArray()[index] = newString;

I convert my list into an array and assign new String. But this line of code does nothing. Is there any solution to it? I think I can just create a new list which will have a different value, but I hope there is a simpler way to achieve that.

Thank you in advance

dekanidze
  • 132
  • 2
  • 9

1 Answers1

2

The List.toArray method creates an array that is a copy of the list. You can use the List.set method to set an item at an index

List<String> list = map.get(key);
list.set(index, newValue);

or, without a temporary variable:

map.get(key).set(index, newValue);
Joni
  • 108,737
  • 14
  • 143
  • 193