Skip to content

Commit 7fc45d6

Browse files
compose & andThen
1 parent c65e918 commit 7fc45d6

File tree

2 files changed

+44
-2
lines changed
  • src/main/java/com/java15/example/modern_java_in_action

2 files changed

+44
-2
lines changed

src/main/java/com/java15/example/modern_java_in_action/Data.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static List<Apple> getData1() {
2121
Apple apple2 = new Apple(1,Colors.RED);
2222
Apple apple3 = new Apple(5,Colors.GREEN);
2323
Apple apple4 = new Apple(3,Colors.RED);
24-
Apple apple5 = new Apple(3,Colors.RED);
24+
Apple apple5 = new Apple(7,Colors.RED);
2525
Apple apple6 = new Apple(5,Colors.GREEN);
2626
List<Apple> apples = new ArrayList<>();
2727
apples.add(apple1);

src/main/java/com/java15/example/modern_java_in_action/ch3_lambdaexpressions/Test.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.java15.example.modern_java_in_action.Apple;
44
import com.java15.example.modern_java_in_action.Colors;
5+
import com.java15.example.modern_java_in_action.Data;
56

67
import java.util.ArrayList;
78
import java.util.Arrays;
89
import java.util.Comparator;
910
import java.util.List;
1011
import java.util.function.*;
1112

13+
import static java.util.Comparator.comparing;
14+
1215
public class Test {
1316

1417
public static int staticValue = 10;
@@ -19,7 +22,46 @@ public static void main(String[] args) {
1922
//extracted2();
2023
//extracted3();
2124
//extracted4(startsWithNumber.test("1he"));
22-
extracted5();
25+
//extracted5();
26+
//extracted6();
27+
//extracted7();
28+
//extracted8();
29+
extracted9();
30+
}
31+
32+
private static void extracted9() {
33+
Function<Integer, Integer> f = x -> x + 1;
34+
Function<Integer, Integer> g = x -> x * 2;
35+
Function<Integer, Integer> h = f.compose(g);
36+
int result = h.apply(1);
37+
System.out.println(result);
38+
}
39+
40+
private static void extracted8() {
41+
Function<Integer, Integer> f = x -> x + 1;
42+
Function<Integer, Integer> g = x -> x * 2;
43+
Function<Integer, Integer> h = f.andThen(g);
44+
int result = h.apply(1);
45+
System.out.println(result);
46+
}
47+
48+
private static void extracted7() {
49+
Data.getData().stream().filter(notRedApple).forEach(System.out::println);
50+
Data.getData().stream().filter(redAppleWithConditions).forEach(System.out::println);
51+
}
52+
53+
static Predicate<Apple> redApple = apple -> Colors.RED.equals(apple.getColor());
54+
static Predicate<Apple> notRedApple = redApple.negate();
55+
static Predicate<Apple> redAppleWithConditions = redApple.and(apple -> apple.getWeight()>2).or(apple ->Colors.GREEN.equals(apple.getColor()));
56+
57+
58+
private static void extracted6() {
59+
List<Apple> list = Data.getData1();
60+
list.sort(comparing(Apple::getWeight).reversed());
61+
list.forEach(System.out::println);
62+
System.out.println("++++++++++++++++++++++++++++++");
63+
list.sort(comparing(Apple::getColor).thenComparing(Apple::getWeight));
64+
list.forEach(System.out::println);
2365
}
2466

2567
private static void extracted5() {

0 commit comments

Comments
 (0)