You can reuse the builder object by clearing it after each iteration
like so:

Person.Builder builder = Person.newBuilder();
for(...) {
  builder.clear();
  builder.setFname(...);
  builder.setLName(...);
  builder.setEmail(...)
  myResponseBuilder.addPerson(builder.build());
}

This is a bit more efficient since you're only creating one new object
per iteration instead of two.  If you're creating a lot of these
objects or this loop is in a critical path, this could be a decent
speedup.

On Dec 11, 12:35 pm, Programmer 09 <macster2...@gmail.com> wrote:
> I am new to ProtocolBuffer and wanted to verify that I am doing the
> right thing here:
>
> I have  proto definitions as:
>
> Person {
>   required string fname =1;
>   required string lname = 2;
>   required string email = 3;
>
> }
>
> MyResponse {
>    repeated Person persons = 1;
>
> }
>
> What is the most effecient way to generate a myResponse with 100
> persons? Is this effecieint?
>
> // assume I have a array of hashmap of person data.
> // there is the myResponseBuilder which is a builder for MyResponse.
>
> for (int i = 0; i < 100; i++)
>    Person.Builder personBuilder = Person.newBuilder();
>    personBuilder.setFname(nameFromMap);
>    personBuilder.setLname(nameFromMap);
>    personBuilder.setEmail(emailFromMap);
>
>    myResponseBuilder.addPerson(personBuilder);
>
> }
>
> Is this the right way to do this? Or is there a better effecient way
> instead of creating a builder for each person data? Any pointers
> appreciated! Thanks!

--

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


Reply via email to