I have a protobuf with a list as one of its members
I want to replace an item in this list.
I tried to remove an item i and add another one at same position i
List<Venues.Category> categoryList = builder.getCategoryList();
categoryList.remove(i);
but I get an unsupported error
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList.remove(Collections.java:1317)
How can I perform the replacement?
I ended up cloning the list, modifying the cloned list and putting it instead of the old one.
List<Venues.Category> clone = categoryList.stream().collect(Collectors.toList());
clone.remove(i);
clone.add(i, modifyCategory(category, countryAbbr, gasStationConfig));
builder.clearCategory();
builder.addAllCategory(clone);
One of the solution is to create a new modifiable list which wraps the old one - I mean passing it to the constructor of e.g. new ArrayList():
List<T> modifiable = new ArrayList<T>(unmodifiable);
Since now, you should be able to remove and add the elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With