Stream reuse in superclass

Jose jgetino at telefonica.net
Thu Apr 4 22:22:56 PDT 2013



So, if I understood you, a rule to adapt old code based on Collections to 
the new paradigm would be something like: 

"try to produce streams instead of collections when possible, but keep on
consuming collections" 


 

-----Mensaje original-----
De: David Holmes [mailto:david.holmes at oracle.com] 
Enviado el: viernes, 05 de abril de 2013 3:31
Para: Jose
CC: lambda-dev at openjdk.java.net
Asunto: Re: Stream reuse in superclass

On 5/04/2013 6:59 AM, Jose wrote:
> I have a class that contains a method that consumes a Stream (previously
was
> a Collection)

And that is your problem. You can not think of operations on a stream 
the way you would a collection, as a stream is not a collection. Your 
class that operated on the elements of the collection would now become 
an operation to be passed to a suitable stream method to be applied to 
each element.

David

> Class Foo<T>{
> 	   bar(Stream<T> s){
>                        .........
> 	}
> }
>
>
> This method is overriden in a subclass, that first consumes the stream and
> then calls super
>
> Class PowerFoo<T> extends Foo<T>{
>     	bar(Stream<T> s){
>       		doImportantThingsWidth(s);
> 		//now the stream is consumed  :-(
>       		super.bar(s)
> 	}
> }
>
>
> What is the recomended way to handle situations like this?.
>
> Curently I'm collecting the stream into a list and getting two streams
from
> it.
>
>



More information about the lambda-dev mailing list