Java: Deleting from lists in protobufs

3,453 views
Skip to first unread message

Dheeraj

unread,
Jun 23, 2010, 1:02:27 AM6/23/10
to Protocol Buffers
How do I delete an entry from a list in a protobuf (repeated field)? I
can add, I can modify, but I cannot delete using any of the generated
APIs. A getList() returns an unmodifiable collection.

Thomas Broyer

unread,
Jun 23, 2010, 6:03:35 AM6/23/10
to Protocol Buffers
Clear the field and then re-add values. You can also set the field
with a List<?> (setField with the FieldDescriptor for your repeated
field), which has the same effect.

Igor Gatis

unread,
Jun 23, 2010, 10:21:22 AM6/23/10
to Thomas Broyer, Protocol Buffers
C++ code has the RemoveLast method. If there is such thing in Java, you could remove 1 item by shifting everything one position earlier and than calling RemoveLast. Batch removal can be also accomplished in the same with two counters.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Dheeraj Pandey

unread,
Jun 23, 2010, 6:01:37 PM6/23/10
to Igor Gatis, Thomas Broyer, Protocol Buffers
Java doesn't have such a thing. Is this by design? If yes, would love to understand the rationale of why clearing-everything-and-reinserting-(N-1)-values is so much better than:
(a) providing an API to do so, and
(b) providing a more optimized implementation that is not O(n) on a delete.

Thanks
Dheeraj

Jason Hsueh

unread,
Jun 23, 2010, 6:30:38 PM6/23/10
to Dheeraj Pandey, Igor Gatis, Thomas Broyer, Protocol Buffers
On Wed, Jun 23, 2010 at 3:01 PM, Dheeraj Pandey <dhe...@alumni.utexas.net> wrote:
Java doesn't have such a thing. Is this by design? If yes, would love to understand the rationale of why clearing-everything-and-reinserting-(N-1)-values is so much better than:
(a) providing an API to do so, and

There is actually a pending change to provide a much nicer API for this sort of modification. It will probably be in the next release, so if you can be patient things should improve soon!
 
(b) providing a more optimized implementation that is not O(n) on a delete.

This would really be up to the application though: it is often the case that repeated fields are order dependent, so deletes may be worst case O(n) in order to shift the elements. Something like SwapElements in the C++ implementation would allow the client to do something more efficient if they do not care about order, though I'm not sure if the aforementioned change will provide this.

Kenton Varda

unread,
Jun 23, 2010, 9:22:54 PM6/23/10
to Jason Hsueh, Dheeraj Pandey, Igor Gatis, Thomas Broyer, Protocol Buffers
On Wed, Jun 23, 2010 at 3:30 PM, Jason Hsueh <jas...@google.com> wrote:
On Wed, Jun 23, 2010 at 3:01 PM, Dheeraj Pandey <dhe...@alumni.utexas.net> wrote:
Java doesn't have such a thing. Is this by design? If yes, would love to understand the rationale of why clearing-everything-and-reinserting-(N-1)-values is so much better than:
(a) providing an API to do so, and

There is actually a pending change to provide a much nicer API for this sort of modification. It will probably be in the next release, so if you can be patient things should improve soon!

I'm not sure this is true.  Are you thinking of Jon's RepeatedFieldBuilder thing?  That's actually more of an implementation detail, not a new public interface.  Although it would make it easier to provide a nicer API as a subsequent change.
 
(b) providing a more optimized implementation that is not O(n) on a delete.

Repeated fields are backed by ArrayLists.  To provide better than O(n) delete we would have to use some other data structure, which would likely be much less efficient for more common usage patterns.  This is, in fact, the whole point:  in the past we provided a remove-by-index operation, and people mistakenly thought that it was somehow more efficient than rebuilding the whole list, even though it wasn't!
Reply all
Reply to author
Forward
0 new messages