-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImperativeApproach.java
More file actions
56 lines (46 loc) · 1.49 KB
/
Copy pathImperativeApproach.java
File metadata and controls
56 lines (46 loc) · 1.49 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
package functionalprogramming;
import java.util.ArrayList;
import java.util.List;
import static functionalprogramming.ImperativeApproach.Gender.FEMALE;
public class ImperativeApproach {
/*
Imperative programming is essentially just you as a developer defining
everything(each step) that you do in your program.
*/
public static void main(String[] args) {
List<Person> people = List.of(
new Person("John", Gender.MALE),
new Person("Sam", FEMALE),
new Person("Stefan", Gender.MALE),
new Person("Bill", Gender.MALE),
new Person("Alex", FEMALE),
new Person("Chrissy", FEMALE),
new Person("Ayesha", FEMALE),
new Person("Vince", Gender.MALE)
);
// Imperative approach for java
/*
Essentially, we are defining everything for something that is very simple
*/
List<Person> females = new ArrayList<>();
for (Person person : people) {
if (Gender.FEMALE.equals(person.gender)) {
females.add(person);
}
}
for(Person female: females) {
System.out.println(female);
}
}
enum Gender {
MALE, FEMALE
}
static class Person {
private final String name;
private final Gender gender;
Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
}
}