1+ package com .winterbe .java8 ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ import java .util .stream .Stream ;
6+
7+ /**
8+ * Testing the order of execution.
9+ *
10+ * @author Benjamin Winterberg
11+ */
12+ public class Streams5 {
13+
14+ public static void main (String [] args ) {
15+ List <String > stringCollection = new ArrayList <>();
16+ stringCollection .add ("ddd2" );
17+ stringCollection .add ("aaa2" );
18+ stringCollection .add ("bbb1" );
19+ stringCollection .add ("aaa1" );
20+ stringCollection .add ("bbb3" );
21+ stringCollection .add ("ccc" );
22+ stringCollection .add ("bbb2" );
23+ stringCollection .add ("ddd1" );
24+
25+ // test1(stringCollection);
26+ // test2(stringCollection);
27+ // test3(stringCollection);
28+ // test4(stringCollection);
29+ // test5(stringCollection);
30+ // test6(stringCollection);
31+ test7 (stringCollection );
32+ }
33+
34+ // stream has already been operated upon or closed
35+ private static void test7 (List <String > stringCollection ) {
36+ Stream <String > stream = stringCollection
37+ .stream ()
38+ .filter (s -> s .toLowerCase ().startsWith ("a" ));
39+
40+ stream .anyMatch (s -> true );
41+ stream .noneMatch (s -> true );
42+ }
43+
44+ // short-circuit
45+ private static void test6 (List <String > stringCollection ) {
46+ stringCollection
47+ .stream ()
48+ .filter (s -> {
49+ System .out .println ("filter: " + s );
50+ return s .toLowerCase ().startsWith ("a" );
51+ })
52+ .map (s -> {
53+ System .out .println ("map: " + s );
54+ return s .toUpperCase ();
55+ })
56+ .anyMatch (s -> true );
57+ }
58+
59+ private static void test5 (List <String > stringCollection ) {
60+ stringCollection
61+ .stream ()
62+ .filter (s -> {
63+ System .out .println ("filter: " + s );
64+ return s .toLowerCase ().startsWith ("a" );
65+ })
66+ .sorted ((s1 , s2 ) -> {
67+ System .out .printf ("sort: %s; %s\n " , s1 , s2 );
68+ return s1 .compareTo (s2 );
69+ })
70+ .map (s -> {
71+ System .out .println ("map: " + s );
72+ return s .toUpperCase ();
73+ })
74+ .forEach (s -> System .out .println ("forEach: " + s ));
75+ }
76+
77+ // sorted = horizontal
78+ private static void test4 (List <String > stringCollection ) {
79+ stringCollection
80+ .stream ()
81+ .sorted ((s1 , s2 ) -> {
82+ System .out .printf ("sort: %s; %s\n " , s1 , s2 );
83+ return s1 .compareTo (s2 );
84+ })
85+ .filter (s -> {
86+ System .out .println ("filter: " + s );
87+ return s .toLowerCase ().startsWith ("a" );
88+ })
89+ .map (s -> {
90+ System .out .println ("map: " + s );
91+ return s .toUpperCase ();
92+ })
93+ .forEach (s -> System .out .println ("forEach: " + s ));
94+ }
95+
96+ private static void test3 (List <String > stringCollection ) {
97+ stringCollection
98+ .stream ()
99+ .filter (s -> {
100+ System .out .println ("filter: " + s );
101+ return s .toLowerCase ().startsWith ("a" );
102+ })
103+ .map (s -> {
104+ System .out .println ("map: " + s );
105+ return s .toUpperCase ();
106+ })
107+ .forEach (s -> System .out .println ("forEach: " + s ));
108+ }
109+
110+ private static void test2 (List <String > stringCollection ) {
111+ stringCollection
112+ .stream ()
113+ .map (s -> {
114+ System .out .println ("map: " + s );
115+ return s .toUpperCase ();
116+ })
117+ .filter (s -> {
118+ System .out .println ("filter: " + s );
119+ return s .toLowerCase ().startsWith ("a" );
120+ })
121+ .forEach (s -> System .out .println ("forEach: " + s ));
122+ }
123+
124+ private static void test1 (List <String > stringCollection ) {
125+ stringCollection
126+ .stream ()
127+ .filter (s -> {
128+ System .out .println ("filter: " + s );
129+ return true ;
130+ })
131+ .forEach (s -> System .out .println ("forEach: " + s ));
132+ }
133+
134+ }
0 commit comments