Re: Puzzler: Fun with Streams.

Posted by Danie... on 13. May 2015

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")));

29. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Sebas...

29. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Sebas...

29. Apr 2015: Re: Survey Request for Research related to Software Engineering and Social Media by Jerry...

29. Apr 2015: Survey Request for Research related to Software Engineering and Social Media by Augus...

29. Apr 2015: Re: Cannot seem to lock HashMap by pww19...

28. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by ram a...

28. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Jan B...

27. Apr 2015: Re: Good example of EJB interface for CRUD operations by Rober...

27. Apr 2015: Easy exhaustive search with Java 8 Streams by Sebas...

27. Apr 2015: Re: Good example of EJB interface for CRUD operations by pavel...

27. Apr 2015: Good example of EJB interface for CRUD operations by pavel...

25. Apr 2015: Re: enum, what is it good for? by Rober...

24. Apr 2015: Re: enum, what is it good for? by Sebas...

24. Apr 2015: Re: enum, what is it good for? by Rober...

22. Apr 2015: Javascript advance by sheik...

22. Apr 2015: What is Armstrong number? Learn about Armstrong Number by sheik...

22. Apr 2015: Re: FileChooser on 2K Displays by j4n b...

22. Apr 2015: FileChooser on 2K Displays by j4n b...

22. Apr 2015: Re: how to hide and show cursor in JTextField? by Eric ...

22. Apr 2015: Re: how to hide and show cursor in JTextField? by karth...

21. Apr 2015: Re: enum, what is it good for? by Kevin...

21. Apr 2015: Re: runnable jar by Arne ...

21. Apr 2015: Re: C++ hash function -> Java by pww19...

21. Apr 2015: Re: Abuse of AssertionError? by Thoma...

21. Apr 2015: Re: Abuse of AssertionError? by j4n b...

21. Apr 2015: runnable jar by cebi ...

21. Apr 2015: Re: Abuse of AssertionError? by Jerry...

21. Apr 2015: Re: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: Abuse of AssertionError? by Jerry...

21. Apr 2015: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: enum, what is it good for? by Marce...

21. Apr 2015: Re: What could be improved about the assert statement by Eric ...

21. Apr 2015: What could be improved about the assert statement by ram a...

21. Apr 2015: Re: enum, what is it good for? by Tassi...

21. Apr 2015: enum, what is it good for? by ram a...

21. Apr 2015: Re: enum, what is it good for? by Leif ...

20. Apr 2015: Re: mouse listener on javafx swing node by Phili...

20. Apr 2015: Re: mouse listener on javafx swing node by Jeff ...

20. Apr 2015: FIGLIO DI PUTTANA NAZIRAZZISTA: PAOLO BARRAI DI CRIMINALI WMO E BSI ITALIA SRL VIA SOCRATE 26 MILANO (OLTRE CHE MEGALAVA CASH MAFIOSO O POLITI-CRIMINALE)! STALKA A MORTE CHI LO FOTOGRAFA E SE "1 INSISTE" IL KILLER PAOLO BARRAI FA "SUICIDARE" DAVVERO! by Memoc...

19. Apr 2015: Re: mouse listener on javafx swing node by Jeff ...

19. Apr 2015: mouse listener on javafx swing node by Phili...

18. Apr 2015: Re: How to extend FilterInputStream to convert tabs to spaces? by Arne ...

16. Apr 2015: Re: QMole Releases Java ECO System for iPhone and iPad by Jerry...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: Re: Java/J2EE Project by Robbi...

15. Apr 2015: Re: new JDK 1.8.0_45 by Wayne...

15. Apr 2015: Re: Calling Royal mail Mailmark web service from java by Arne ...

15. Apr 2015: Re: presentable Java features and programs by Jeff ...

15. Apr 2015: Re: presentable Java features and programs by Jeff ...

14. Apr 2015: What is this? (Java syntax) by ram a...

14. Apr 2015: new JDK 1.8.0_45 by Roedy...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: Re: What is this? (Java syntax) by Eric ...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: New Requirment Hadoop Developer / Java Developer at Phoenix, AZ by Moin ...

14. Apr 2015: Fix my game? Help! by 12ich...

14. Apr 2015: Re: NotSerializableException by Eric ...

14. Apr 2015: Re: NotSerializableException by Rober...

14. Apr 2015: Java/J2EE Project by rahul...

13. Apr 2015: NotSerializableException by akksh...

13. Apr 2015: Calling Royal mail Mailmark web service from java by loial...

13. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marce...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marti...

12. Apr 2015: Re: presentable Java features and programs by Arne ...

12. Apr 2015: Re: presentable Java features and programs by Arne ...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Andre...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Arne ...

12. Apr 2015: SCHIFOSO PUZZONE NEOPIDUISTA TIZIANO RENZI (WIKI, IL FATTO QUOTIDIANO, ECT) FA DA TRAMITE FRA NAZIMAFIOSO PEDOFILO STRAGISTA SILVIO BERLUSCONI, FASCIO-LESBICA MARINA BERLUSCONI E FIGLIO BERLUS-CO-RROTTISSIMO MATTEO RENZI PER FAR SI CHE BERLUSCONIA... by Memoc...

11. Apr 2015: Re: http://www.w3.org/TR/html5/obsolete.html#non-conforming-features by Arne ...

11. Apr 2015: http://www.w3.org/TR/html5/obsolete.html#non-conforming-features by Al he...

10. Apr 2015: Re: Return of the Applets? by Silvi...

10. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marce...

10. Apr 2015: Re: Return of the Applets? by ram a...

10. Apr 2015: Re: how to set LD_LIBRARY_PATH within the Java program? by Andre...

10. Apr 2015: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Andre...

10. Apr 2015: Check out my Toc Java tool, ideal for DevOps! by morti...

09. Apr 2015: Re: how to set LD_LIBRARY_PATH within the Java program? by A Bol...

09. Apr 2015: Re: how to set LD_LIBRARY_PATH within the Java program? by Eric ...

09. Apr 2015: how to set LD_LIBRARY_PATH within the Java program? by John ...

09. Apr 2015: Разработчика систем видеоконференцсвязи. by Таня ...

08. Apr 2015: Re: Return of the Applets? by Kevin...

08. Apr 2015: Re: Return of the Applets? by Kevin...

08. Apr 2015: Re: presentable Java features and programs by Jeff ...

08. Apr 2015: Re: Any good book/website with samples how to run Java in web applications? by Rober...

08. Apr 2015: Re: Any good book/website with samples how to run Java in web applications? by Robbi...

08. Apr 2015: ASSASSINO FIGLIO DI PUTTANA: PAOLO BARRAI (28.06.1965)! DI CRIMINALISSIME WMO E BSI ITALIA SRL (OLTRE CHE MEGALAVA CASH MAFIOSO E/O POLITI-C-RIMINALE)! STALKA A MORTE CHI LO FOTOGRAFA E SE "UNO" INSISTE IL KILLER PAOLO BARRAI FA "SUICIDARE" DAVVERO!! by Memoc...

08. Apr 2015: Re: presentable Java features and programs by Gene ...

07. Apr 2015: Re: Return of the Applets? by Marti...

07. Apr 2015: Re: presentable Java features and programs by Jeff ...

07. Apr 2015: Re: Return of the Applets? by Marti...

07. Apr 2015: Re: Return of the Applets? by Marti...

07. Apr 2015: Re: Return of the Applets? by Silvi...

07. Apr 2015: Re: Return of the Applets? by Jerry...

07. Apr 2015: Re: Return of the Applets? by Jerry...

07. Apr 2015: Re: Return of the Applets? by Jerry...

07. Apr 2015: Re: Return of the Applets? by Jerry...