Skip to content

Commit 96e725b

Browse files
Changes
1 parent b42b474 commit 96e725b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public Apple(int weight,String color) {
1313
this.weight = weight;this.color = color;
1414
}
1515

16+
public Apple() {
17+
18+
}
19+
1620
public Integer getWeight() {
1721
return weight;
1822
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.java15.example.modern_java_in_action.Apple;
44
import com.java15.example.modern_java_in_action.Colors;
55

6+
import java.util.ArrayList;
67
import java.util.Arrays;
78
import java.util.Comparator;
89
import java.util.List;
@@ -18,7 +19,25 @@ public static void main(String[] args) {
1819
//extracted2();
1920
//extracted3();
2021
//extracted4(startsWithNumber.test("1he"));
22+
extracted5();
23+
}
2124

25+
private static void extracted5() {
26+
Supplier<Apple> appleSupplier = Apple::new;
27+
System.out.println(appleSupplier.get());
28+
Function<Integer,Apple> appleFunction = Apple::new;
29+
System.out.println(appleFunction.apply(10));
30+
BiFunction<Integer,String,Apple> appleBiFunction = (i,c)->new Apple(i,c);
31+
System.out.println(appleBiFunction.apply(10,Colors.RED));
32+
List<Integer> weights = Arrays.asList(7, 3, 4, 10);
33+
map(weights, Apple::new).forEach(System.out::println);
34+
}
35+
static public List<Apple> map(List<Integer> list, Function<Integer, Apple> f) {
36+
List<Apple> result = new ArrayList<>();
37+
for(Integer i: list) {
38+
result.add(f.apply(i));
39+
}
40+
return result;
2241
}
2342

2443
private static void extracted4(boolean test) {
@@ -28,9 +47,7 @@ private static void extracted4(boolean test) {
2847
static Predicate<String> startsWithNumber = (String string) -> Test.startsWithNumber(string);
2948

3049
static private Boolean startsWithNumber(String value) {
31-
if(value.contains("1"))
32-
return true;
33-
return false;
50+
return value.contains("1");
3451
}
3552

3653
private static void extracted3() {

0 commit comments

Comments
 (0)