Iterator<HelloResponse> responseIterator = blockingStub.hello(helloRequest);
while (responseIterator.hasNext()) {
doStuff(responseIterator.next());
}
Iterable<HelloResponse> responseIterable = blockingStub.hello(helloRequest);
for (HelloResponse response : responseIterable) {
doStuff(response);
}
Only con against Iterable could be that you can invoke iterator() many times.
--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/b87ca627-c279-408f-9c4f-a7ffb7c55bf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Only con against Iterable could be that you can invoke iterator() many times.I think this is in fact the entire reason that it returns Iterator instead of Iterable. The stream can't be iterated more than once, so Iterator is the correct representation.
It cannot implement Collection because the actual number of elements is not known up-front (Collection provides a size() method that could not be implemented). Clients and servers just stream messages until they are done without having to report the number at the start of the operation.
BTW, I don't see anything wrong in your code snippet. In fact, the Iterator sample is more concise and just as readable.
Only con against Iterable could be that you can invoke iterator() many times.I think this is in fact the entire reason that it returns Iterator instead of Iterable. The stream can't be iterated more than once, so Iterator is the correct representation.Why you can't iterate stream multiple times?
It cannot implement Collection because the actual number of elements is not known up-front (Collection provides a size() method that could not be implemented). Clients and servers just stream messages until they are done without having to report the number at the start of the operation.In this case Collection.size() would be blocking - wait until get everything. Hibernate does something similar when you're doing size() on lazy collection.
BTW, I don't see anything wrong in your code snippet. In fact, the Iterator sample is more concise and just as readable.Both code snippests works 100% fine. just in my opinion for each looks better, using bare iterator is not frequent.