Skip to content

Commit 335d5c5

Browse files
committed
be affected
1 parent 98232ba commit 335d5c5

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

src/main/java/com/winterbe/java8/samples/lambda/Lambda3.java

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,66 @@
1616
*/
1717
public class Lambda3 {
1818

19-
@FunctionalInterface
20-
interface Fun {
21-
void foo();
22-
}
19+
@FunctionalInterface
20+
interface Fun {
21+
void foo();
22+
}
2323

24-
public static void main(String[] args) throws Exception {
24+
public static void main(String[] args) throws Exception {
2525

26-
// Predicates
26+
// Predicates
27+
Predicate<String> predicate = (s) -> s.length() > 0;
2728

28-
Predicate<String> predicate = (s) -> s.length() > 0;
29+
predicate.test("foo"); // true
30+
predicate.negate().test("foo"); // false
2931

30-
predicate.test("foo"); // true
31-
predicate.negate().test("foo"); // false
32+
Predicate<Boolean> nonNull = Objects::nonNull;
33+
nonNull.test(null);
3234

33-
Predicate<Boolean> nonNull = Objects::nonNull;
34-
Predicate<Boolean> isNull = Objects::isNull;
35+
Predicate<Boolean> isNull = Objects::isNull;
36+
isNull.test(null);
3537

36-
Predicate<String> isEmpty = String::isEmpty;
37-
Predicate<String> isNotEmpty = isEmpty.negate();
38+
Predicate<String> isEmpty = String::isEmpty;
39+
isEmpty.test("");
3840

41+
Predicate<String> isNotEmpty = isEmpty.negate();
42+
isNotEmpty.test("");
43+
44+
// Functions
45+
Function<String, Integer> toInteger = Integer::valueOf;
46+
Function<String, String> backToString = toInteger.andThen(String::valueOf);
3947

40-
// Functions
48+
backToString.apply("123"); // "123"
4149

42-
Function<String, Integer> toInteger = Integer::valueOf;
43-
Function<String, String> backToString = toInteger.andThen(String::valueOf);
50+
// Suppliers
4451

45-
backToString.apply("123"); // "123"
52+
Supplier<Person> personSupplier = Person::new;
53+
personSupplier.get(); // new Person
4654

55+
// Consumers
4756

48-
// Suppliers
57+
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
58+
greeter.accept(new Person("Luke", "Skywalker"));
4959

50-
Supplier<Person> personSupplier = Person::new;
51-
personSupplier.get(); // new Person
60+
// Comparators
5261

62+
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
5363

54-
// Consumers
64+
Person p1 = new Person("John", "Doe");
65+
Person p2 = new Person("Alice", "Wonderland");
5566

56-
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
57-
greeter.accept(new Person("Luke", "Skywalker"));
67+
comparator.compare(p1, p2); // > 0
68+
comparator.reversed().compare(p1, p2); // < 0
5869

70+
// Runnables
5971

72+
Runnable runnable = () -> System.out.println(UUID.randomUUID());
73+
runnable.run();
6074

61-
// Comparators
75+
// Callables
6276

63-
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
64-
65-
Person p1 = new Person("John", "Doe");
66-
Person p2 = new Person("Alice", "Wonderland");
67-
68-
comparator.compare(p1, p2); // > 0
69-
comparator.reversed().compare(p1, p2); // < 0
70-
71-
72-
// Runnables
73-
74-
Runnable runnable = () -> System.out.println(UUID.randomUUID());
75-
runnable.run();
76-
77-
78-
// Callables
79-
80-
Callable<UUID> callable = UUID::randomUUID;
81-
callable.call();
82-
}
77+
Callable<UUID> callable = UUID::randomUUID;
78+
callable.call();
79+
}
8380

8481
}

src/main/java/com/winterbe/java8/samples/lambda/Lambda4.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ public class Lambda4 {
1212
void testScopes() {
1313
int num = 1;
1414

15-
Lambda2.Converter<Integer, String> stringConverter =
15+
LambdaToInterface.Converter<Integer, String> stringConverter =
1616
(from) -> String.valueOf(from + num);
1717

1818
String convert = stringConverter.convert(2);
1919
System.out.println(convert); // 3
2020

21-
Lambda2.Converter<Integer, String> stringConverter2 = (from) -> {
21+
LambdaToInterface.Converter<Integer, String> stringConverter2 = (from) -> {
2222
outerNum = 13;
2323
return String.valueOf(from);
2424
};
2525

2626
String[] array = new String[1];
27-
Lambda2.Converter<Integer, String> stringConverter3 = (from) -> {
27+
LambdaToInterface.Converter<Integer, String> stringConverter3 = (from) -> {
2828
array[0] = "Hi there";
2929
return String.valueOf(from);
3030
};

0 commit comments

Comments
 (0)