Skip to content

Commit 1e234c3

Browse files
committed
harmonize funcitonal programming
1 parent 132528d commit 1e234c3

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

java2/functionalprogramming/AgeComparator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
public class AgeComparator implements Comparator<Human> {
66
public int compare(Human h1, Human h2) {
7-
if (h1.age() > h1.age()) {
7+
if (h1.age() < h2.age()) {
8+
return -1;
9+
} else if (h1.age() > h2.age()) {
810
return 1;
911
} else {
10-
return -1;
12+
return 0;
1113
}
1214
}
1315
}

java2/functionalprogramming/Company.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
import java.util.List;
77

88
public class Company {
9-
private List<Human> employees;
9+
public final List<Human> employees;
1010

1111
public Company() {
12-
this.employees = new ArrayList<>();
13-
this.employees.add(new Human(80));
14-
this.employees.add(new Human(10));
15-
this.employees.add(new Human(65));
12+
this.employees = new ArrayList<>(List.of(new Human(80), new Human(10), new Human(65)));
1613
}
1714

1815
public void sortByAgeDescending() {
@@ -24,32 +21,36 @@ public void sortByAgeDescending() {
2421
employees,
2522
new Comparator<Human>() {
2623
public int compare(Human h1, Human h2) {
27-
if (h1.age() > h1.age()) {
24+
if (h1.age() < h2.age()) {
25+
return -1;
26+
} else if (h1.age() > h2.age()) {
2827
return 1;
2928
} else {
30-
return -1;
29+
return 0;
3130
}
3231
}
3332
});
3433

35-
// Lambda Funktion - Lange Syntax
34+
// // Lambda Funktion - Lange Syntax
3635
Collections.sort(
3736
employees,
3837
(Human h1, Human h2) -> {
39-
if (h1.age() > h1.age()) {
38+
if (h1.age() < h2.age()) {
39+
return -1;
40+
} else if (h1.age() > h2.age()) {
4041
return 1;
4142
} else {
42-
return -1;
43+
return 0;
4344
}
4445
});
45-
// Lambda Funktion - Kurze Syntax
46-
Collections.sort(employees, (Human h1, Human h2) -> h1.age() > h1.age() ? 1 : -1);
46+
// // Lambda Funktion - Kurze Syntax
47+
Collections.sort(employees, (Human h1, Human h2) -> h1.age() < h2.age() ? -1 : h1.age() > h2.age() ? 1 : 0);
4748

48-
// Lambda Funktion - Kurze Syntax ohne explizite Datentypen
49-
Collections.sort(employees, (h1, h2) -> h1.age() > h1.age() ? 1 : -1);
49+
// // Lambda Funktion - Kurze Syntax ohne explizite Datentypen
50+
Collections.sort(employees, (h1, h2) -> h1.age() < h2.age() ? -1 : h1.age() > h2.age() ? 1 : 0);
5051

51-
// Lambda Funktion - Als Referenzvariable gespeichert
52-
Comparator<Human> ageSorter = (h1, h2) -> h1.age() > h1.age() ? 1 : -1;
52+
// // Lambda Funktion - Als Referenzvariable gespeichert
53+
Comparator<Human> ageSorter = (h1, h2) -> h1.age() < h2.age() ? -1 : h1.age() > h2.age() ? 1 : 0;
5354
Collections.sort(employees, ageSorter);
5455
}
5556
}

0 commit comments

Comments
 (0)