-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStudentStreamExample.java
52 lines (41 loc) · 1.72 KB
/
StudentStreamExample.java
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
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Student {
String firstName;
String lastName;
int rollNumber;
char division;
public Student(String firstName, String lastName, int rollNumber, char division) {
this.firstName = firstName;
this.lastName = lastName;
this.rollNumber = rollNumber;
this.division = division;
}
}
public class StudentStreamExample {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("Vikram","Gupta",1,'A'));
studentList.add(new Student("Vivek","Gupta",11,'A'));
studentList.add(new Student("Vishal","Gupta",13,'C'));
studentList.add(new Student("Vinod","Gupta",4,'D'));
studentList.add(new Student("Vikram","Gupta",5,'B'));
Stream<Student> studentStream = studentList.stream();
Stream<Student> studentStream1 = studentStream.filter((student) -> {
return student.rollNumber > 10;
});
studentStream1.forEach(student -> System.out.println("First Name : " + student.firstName+
" Roll Number: " + student.rollNumber));
Stream<Student> studentStreamNew = studentList.stream();
System.out.println(studentStreamNew.count());
Stream<Student> studentStreamNew2 = studentList.stream();
Stream<Integer> rollNumberStream = studentStreamNew2.map(student -> {
return student.rollNumber;
});
//Integer::intValue --> ClassName :: methodName
System.out.println("Sum of IDs: "+ rollNumberStream.mapToInt(Integer::intValue).sum());
}
}