Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
148 lines (123 loc) · 5.01 KB

File metadata and controls

148 lines (123 loc) · 5.01 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Practicing JavaStreams
package test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class JavaStreams {
public static void main(String[] args) throws IOException{
//0. Integer Stream
IntStream
.range(1,10).
forEach(System.out::print);
System.out.println("\n");
//1. Integer Stream with skip
System.out.println(
IntStream
.range(5,11) //add 5+6+7+8+9+10
.skip(4) //skip first 4 elements
.sum());
System.out.println("\n");
//2. Integer Stream with skip
IntStream
.range(1,10)
.skip(5) //skip first 5 elements
.forEach(x -> System.out.print(x));
System.out.println("\n");
//3. Integer Stream with sum
System.out.println(
IntStream
.range(1,10)
.sum());
System.out.println();
//4. Stream.of, sorted and then findFirst item
Stream.of("Ava","Aneri","Alberto")
.sorted()
.findFirst()
.ifPresent(System.out::println);
System.out.println();
//5. Stream from Array, sort, filter and print
String[] names = {"Al", "Ankit", "Kushal", "Brunt", "Sarika", "amanda", "Hans", "Shivika", "xyz", "shamili"};
Arrays.stream(names) //same as Stream.of(names)
.filter(x -> x.startsWith("S"))
.sorted()
.forEach(System.out::println);
System.out.println();
//6. average of squares of an int array
Arrays.stream(new int[] {2,4,6,8,10})
.map(x->x*x)
.average()
.ifPresent(System.out::println);
System.out.println();
//7. Stream from List, filter and print
List<String> people = Arrays.asList("Al", "Ankit", "Kushal", "Brunt", "Sarika", "amanda", "Hans", "Shivika", "xyz", "shamili");
people
.stream()
.map(String::toLowerCase)
.filter(x -> x.startsWith("a"))
.forEach(System.out::println);
System.out.println();
//8. streams rows from text file, sort, filter and print with more than 15 character
Stream<String> cricketer = Files.lines(Paths.get("cricketer.txt")); //kindly provide your own file location
//for example :- Stream<String> rows1 = Files.lines(Paths.get("C:\\Users\\Aaditya Raj\\eclipse-workspace\\CodePractice\\bin\\test\\cricketer.txt));
cricketer
.sorted()
.filter(x -> x.length() > 15)
.forEach(System.out::println);
cricketer.close();
System.out.println();
//9. streams rows from text file, sort, filter and print substring
List<String> cricketer2 = (List<String>) Files.lines(Paths.get("cricketer.txt")) //kindly provide your own file location
.filter(x -> x.contains("ja"))
.collect(Collectors.toList());
cricketer2.forEach(x -> System.out.println(x));
System.out.println();
//10. Stream rows from CSV file and filter out which rows don't have 3 item array and count all which have 3 items in it.
Stream<String> rows1 = Files.lines(Paths.get("data.txt")); //kindly provide your own file location
int rowCount = (int) rows1
.map(x -> x.split(","))
.filter(x -> x.length == 4)
.count();
System.out.println(rowCount + "rows.");
rows1.close();
System.out.println();
//11. Stream rows from CSV file, and parse data from rows
Stream<String> rows2 = Files.lines(Paths.get("data.txt")); //kindly provide your own file location
rows2
.map(x -> x.split(", "))
.filter(x -> x.length == 4)
.filter(x -> Integer.parseInt(x[1]) > 3)
.forEach(x -> System.out.println(x[0]+ " " +x[1]+ " " +x[2]));
rows2.close();
System.out.println();
//12. Stream rows from CSV file, store fields in HashMap as key-value pair
Stream<String> rows3 = Files.lines(Paths.get("data.txt")); //kindly provide your own file location
Map<String, Integer> map = new HashMap<>();
map = rows3
.map(x -> x.split(", "))
.filter(x -> x.length == 4)
.filter(x -> Integer.parseInt(x[1]) > 3)
.collect(Collectors.toMap(x->x[0], x-> Integer.parseInt(x[1])));
rows3.close();
for(String key : map.keySet()) {
System.out.println(key + " " + map.get(key));
}
System.out.println();
//13. Reduction - sum
double total = Stream.of(7.3, 1.5, 4.8)
.reduce(0.0, (Double a, Double b) -> a+b);
System.out.println("Total = "+total);
System.out.println();
//14. Reduction - summary statistics
IntSummaryStatistics summary = IntStream.of(7, 2, 19, 88, 73, 4, 10)
.summaryStatistics();
System.out.println(summary);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.