Skip to content

Commit 2a72156

Browse files
authored
added Java 8 Streams files
1 parent 09bf821 commit 2a72156

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

Java 8 Streams/JavaStreams.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

Java 8 Streams/bands.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Rolling Stones
2+
Lady Gaga
3+
Jackson Browne
4+
Maroon 5
5+
Arijit Singh
6+
Elton John
7+
John Mayer
8+
CCR
9+
Eagles
10+
Pink
11+
Aerosmith
12+
Adele
13+
Taylor Swift
14+
Faye Wong
15+
Bob Seger
16+
ColdPlay
17+
Boston
18+
The Cars
19+
Cheap Trick
20+
Def Leppard
21+
Ed Sheeran
22+
Dire Straits
23+
Train
24+
Tom Petty
25+
Jack Johnson
26+
Jimmy Buffett
27+
Mumford and Sons
28+
Phil Collins
29+
Rod Stewart
30+
The Script
31+
Elvis
32+
Michael Buble

Java 8 Streams/data.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
A,12,3.7
2+
B,17,2.8
3+
C,14,1.9
4+
D,23,2.7
5+
E
6+
F,18,3.4

0 commit comments

Comments
 (0)