-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
70 lines (56 loc) · 1.65 KB
/
Main.java
File metadata and controls
70 lines (56 loc) · 1.65 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
65
66
67
68
package imperative;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main
{
public static void main(String[] args) {
List<Person> people = List.of(
new Person("Alex",Gender.MALE),
new Person("Faith",Gender.FEMALE),
new Person("Joe",Gender.MALE),
new Person("Riley",Gender.MALE),
new Person("Rose",Gender.FEMALE)
);
// Imperative Approach
System.out.println("Imperative Approach...");
List<Person> females = new ArrayList<Person>();
for (Person person : people)
{
if (Gender.FEMALE.equals(person.gender))
{
females.add(person);
}
}
for (Person female : females)
{
System.out.println(female);
}
// Declarative Approach
System.out.println("Declarative Approach");
List<Person> female2 = people.stream()
.filter(person -> Gender.FEMALE.equals(person.gender))
.collect(Collectors.toList());
female2.forEach(System.out::println);
}
static class Person
{
private final String name;
private final Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender=" + gender +
'}';
}
}
enum Gender
{
MALE,FEMALE
}
}