-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMaxAge.java
More file actions
64 lines (44 loc) · 1.25 KB
/
FindMaxAge.java
File metadata and controls
64 lines (44 loc) · 1.25 KB
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
package com.interview.stream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
class User {
public int age;
public String name;
public User(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
}
}
public class FindMaxAge{
public static void main(String[] args) {
List<User> l = new ArrayList<User>();
l.add(new User(24,"Max"));
l.add(new User(34,"Aux"));
l.add(new User(44,"John"));
User maxageUser = l.stream().max(Comparator.comparing(User :: getAge)).get();
System.out.println(maxageUser);
User minAgeUser = l.stream().min(Comparator.comparing(User::getAge)).get();
System.out.println(minAgeUser);
List<String> str = l.stream().map(User::getName).filter(name->name.startsWith("J")).collect(Collectors.toList());
System.out.println(str);
l.stream().mapToInt(age->age.getAge()).average().ifPresent(age->System.out.println("age avg is: "+age));
}
}