Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove an item from a UnmodifiableList in a protobuf

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?

like image 329
Elad Benda Avatar asked Dec 02 '25 13:12

Elad Benda


2 Answers

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);
like image 102
Elad Benda Avatar answered Dec 05 '25 01:12

Elad Benda


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.

like image 37
Wojciech Kazior Avatar answered Dec 05 '25 01:12

Wojciech Kazior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!