Convenient methods in old Iterable
paul.sandoz at oracle.com
Fri Oct 19 04:46:26 PDT 2012
On Oct 19, 2012, at 1:21 PM, Marcos Antonio <marcos_antonio_ps at hotmail.com> wrote:
> Thank you for the answer, Paul. What I meant was that I had code like this: Iterable<Employee> i = employees.filter(...); Now I called i.isEmpty() or i.count(). The code was converted to this: Stream<Employee> s = employees.stream().filter(...); What is the best way to know now if the stream is empty or its count? Thank you. Marcos > Subject: Re: Convenient methods in old Iterable
To determine if the stream is empty you can do:
but note that will commit the stream to being pulled based for any further operations on the stream.
To determine the size you have to stuff it into a collection or convert to an array.
I am not very familiar with the old API, but i strongly suspect the implementation would have internally created a collection to determine the size.
>> From: paul.sandoz at oracle.com
>> Date: Fri, 19 Oct 2012 13:00:44 +0200
>> CC: lambda-dev at openjdk.java.net
>> To: marcos_antonio_ps at hotmail.com
>> On Oct 19, 2012, at 3:53 AM, Marcos Antonio <marcos_antonio_ps at hotmail.com> wrote:
>>> Hello! The methods isEmpty() and count() in Iterable were very convenient to me.
>> An Iterable may not know it's size, or the elements of the Iterable cannot be efficiently counted. See the Sized interface.
>>> Now with Stream do I always need to convert the stream to a collection or an array (into() and toArray() methods) to get the same functionality or is there an easier way? Thank you.
>> You might be able to check if the Iterable is an instance of Sized.
More information about the lambda-dev