Remove objects from a list contained in a protobuf generated class

Viewed 99

My protobuf object looks like this.

message Abc {
   repeated XYZ xyz;
}

When this gets converted to a class Abc, it has following methods for the list<XYZ>;

getXyzList(), getXyzBuilderList(), getXyzOrBuilderList()

Lets say this list contains 4 objects of class XYZ, and I iterate over this list. Based on a condition I need to remove specific objects from this list.

Since while iterating over the list I can't remove the objects, I add the objects into another list e.g. say tempList (of type List<XYZ>).

Now lets assume that tempList has 2 objects from the list. What is the best way to remove these 2 objects from the original list?

I can't do Abc.getXyzList() or Abc.getXyzOrBuilderList() since it is immutable. If I do Abc.getXyzBuilderList(), it changes the object references (keeping contents same).

If I do Abc.getXyzBuilderList().removeAll(tempList), the object references from tempList and Abc.getXyzBuilderList() are different so the objects are not removed.

How can I get around this problem?

0 Answers
Related