-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.java
51 lines (50 loc) · 1.95 KB
/
stream.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
import java.util.*;
import java.util.stream.Collectors;
public class stream {
public static void main(String[] args)
{
Student st1 = new Student("Anna", 16, 'f');
Student st2 = new Student("Maksim", 19, 'm');
Student st3 = new Student("Olga", 21, 'f');
List<Student> students = new ArrayList<>();
students.add(st1);
students.add(st2);
students.add(st3);
System.out.println(students);
System.out.println("-------------------------------------------");
List<Student> filteredtudents = students.stream().filter(e->e.age>17&&e.name.length()>4).collect(Collectors.toList());
System.out.println(filteredtudents);
System.out.println("-------------------------------------------");
List<Integer> mappedstudents = students.stream().map(e->e.name.length()).collect(Collectors.toList());
System.out.println(mappedstudents);
System.out.println("-------------------------------------------");
int[] array = {5, 12, 2, 51, 52, 42};
Arrays.stream(array).forEach(e->System.out.println(e));
System.out.println("-------------------------------------------");
Arrays.stream(array).forEach(System.out::println);
System.out.println("-------------------------------------------");
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(6);
list.add(2);
Optional<Integer> o = list.stream().reduce((a, e) -> a*e);
if (o.isPresent()){System.out.println(o.get());}
else {System.out.println("null");}
System.out.println("-------------------------------------------");
}
}
class Student {
String name;
int age;
char sex;
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return name+", "+age+", "+sex;
}
}