1+ import java .lang .String ;
2+ import java .util .Arrays ;
3+ import java .util .List ;
4+ import java .util .stream .*;
5+ import java .util .*;
6+ import java .nio .file .*;
7+ import java .io .IOException ;
8+
9+ public class JavaStreams {
10+ public static void main (String [] args ) throws IOException {
11+ // 1. Integer Stream
12+ IntStream
13+ .range (1 , 10 )
14+ .forEach (System .out ::print );
15+ System .out .println ();
16+
17+ // 2. Integer Stream with skip
18+ IntStream
19+ .range (1 , 10 )
20+ .skip (5 )
21+ .forEach (x -> System .out .println (x ));
22+ System .out .println ();
23+
24+ // 3. Integer Stream with sum
25+ System .out .println (
26+ IntStream
27+ .range (1 , 5 )
28+ .sum ());
29+ System .out .println ();
30+
31+ // 4. Stream.of, sorted and findFirst
32+ Stream .of ("Ava" , "Aneri" , "Alberto" )
33+ .sorted ()
34+ .findFirst ()
35+ .ifPresent (System .out ::println );
36+
37+ // 5. Stream from Array, sort, filter and print
38+ String [] names = {"Al" , "Ankit" , "Kushal" , "Brent" , "Sarika" , "amanda" , "Hans" , "Shivika" , "Sarah" };
39+ Arrays .stream (names ) // same as Stream.of(names)
40+ .filter (x -> x .startsWith ("S" ))
41+ .sorted ()
42+ .forEach (System .out ::println );
43+
44+ // 6. average of squares of an int array
45+ Arrays .stream (new int [] {2 , 4 , 6 , 8 , 10 })
46+ .map (x -> x * x )
47+ .average ()
48+ .ifPresent (System .out ::println );
49+
50+ // 7. Stream from List, filter and print
51+ List <String > people = Arrays .asList ("Al" , "Ankit" , "Brent" , "Sarika" , "amanda" , "Hans" , "Shivika" , "Sarah" );
52+ people
53+ .stream ()
54+ .map (String ::toLowerCase )
55+ .filter (x -> x .startsWith ("a" ))
56+ .forEach (System .out ::println );
57+
58+ // 8. Stream rows from text file, sort, filter, and print
59+ Stream <String > bands = Files .lines (Paths .get ("bands.txt" ));
60+ bands
61+ .sorted ()
62+ .filter (x -> x .length () > 13 )
63+ .forEach (System .out ::println );
64+ bands .close ();
65+
66+ // 9. Stream rows from text file and save to List
67+ List <String > bands2 = Files .lines (Paths .get ("bands.txt" ))
68+ .filter (x -> x .contains ("jit" ))
69+ .collect (Collectors .toList ());
70+ bands2 .forEach (x -> System .out .println (x ));
71+
72+ // 10. Stream rows from CSV file and count
73+ Stream <String > rows1 = Files .lines (Paths .get ("data.txt" ));
74+ int rowCount = (int )rows1
75+ .map (x -> x .split ("," ))
76+ .filter (x -> x .length == 3 )
77+ .count ();
78+ System .out .println (rowCount + " rows." );
79+ rows1 .close ();
80+
81+ // 11. Stream rows from CSV file, parse data from rows
82+ Stream <String > rows2 = Files .lines (Paths .get ("data.txt" ));
83+ rows2
84+ .map (x -> x .split ("," ))
85+ .filter (x -> x .length == 3 )
86+ .filter (x -> Integer .parseInt (x [1 ]) > 15 )
87+ .forEach (x -> System .out .println (x [0 ] + " " + x [1 ] + " " + x [2 ]));
88+ rows2 .close ();
89+
90+ // 12. Stream rows from CSV file, store fields in HashMap
91+ Stream <String > rows3 = Files .lines (Paths .get ("data.txt" ));
92+ Map <String , Integer > map = new HashMap <>();
93+ map = rows3
94+ .map (x -> x .split ("," ))
95+ .filter (x -> x .length == 3 )
96+ .filter (x -> Integer .parseInt (x [1 ]) > 15 )
97+ .collect (Collectors .toMap (
98+ x -> x [0 ],
99+ x -> Integer .parseInt (x [1 ])));
100+ rows3 .close ();
101+ for (String key : map .keySet ()) {
102+ System .out .println (key + " " + map .get (key ));
103+ }
104+
105+ // 13. Reduction - sum
106+ double total = Stream .of (7.3 , 1.5 , 4.8 )
107+ .reduce (0.0 , (Double a , Double b ) -> a + b );
108+ System .out .println ("Total = " + total );
109+
110+ // 14. Reduction - summary statistics
111+ IntSummaryStatistics summary = IntStream .of (7 , 2 , 19 , 88 , 73 , 4 , 10 )
112+ .summaryStatistics ();
113+ System .out .println (summary );
114+ }
115+ }
0 commit comments