Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31701

Re: Puzzler: Fun with Streams.

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Puzzler: Fun with Streams.
References <Nfy4x.122164$566.49072@fx20.iad> <mivfm2$hit$1@dont-email.me>
Message-ID <IUN4x.35814$yw3.5574@fx29.iad> (permalink)
Date 2015-05-13 12:49 -0700

Show all headers | View raw


On 5/13/15 5:21 AM, Jeff Higgins wrote:
> On 05/12/2015 10:01 PM, Daniel Pitts wrote:
>> It occurred to me that I could write this little snippet someplace, and
>> it would work.
>>
>>      return stream.filter(o->o<-o);
>>
>> stream is an object which implements java.util.stream.BaseStream, though
>> I won't tell you which one
>>
>> Might be obvious to everyone, but I thought it was a good little puzzler.
>>
>> What input do you think this will work with, and what is the result?
>
> package example;
>
> import java.util.Arrays;
> import java.util.stream.IntStream;
>
> public class FilterTest {
>
>    public static void main(String[] args) {
>      int[] test = new int[]{-2,-1,0,1,2};
>      filter(test).forEach(p -> System.out.println(p));
>
>    }
>    static IntStream filter(int[] t) {
>      IntStream stream = Arrays.stream(t);
>      return stream.filter(o->o<-o);
>    }
>
> }

Yep. though you should use forEachOrdered, and you can use 
System.out::println

    filter(test).forEachOrdered(System.out::println);

Though I prefer to use collect, to keep parallelism support:

    System.out.println(filter(test).collect(Collectors.joining("\n")));

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Puzzler: Fun with Streams. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-05-12 19:01 -0700
  Re: Puzzler: Fun with Streams. Jeff Higgins <jeff@invalid.invalid> - 2015-05-13 08:21 -0400
    Re: Puzzler: Fun with Streams. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-05-13 12:49 -0700
  Re: Puzzler: Fun with Streams. scattered <rescattered@gmail.com> - 2015-05-18 05:49 -0700
    Re: Puzzler: Fun with Streams. scattered <rescattered@gmail.com> - 2015-05-18 06:29 -0700
  Re: Puzzler: Fun with Streams. javadiscover@gmail.com - 2015-06-10 03:19 -0700

csiph-web