【发布时间】:2019-09-23 10:19:05
【问题描述】:
表达式 this.apply(stringValue) 在 lambda 表达式 j8Test.PerformChange$$Lambda$2/284720968@2f4d3709 上返回 true
为什么它返回真而不是假。为什么它实际上根本没有返回任何东西? (我不是在谈论 PerformChangeImpl 类型的所有其他对象)
private final List<PerformChange> performChangeList = Arrays.asList(
new PerformChangeImpl("1"),
new PerformChangeImpl("2"),
new PerformChangeImpl("3")
);
@FunctionalInterface
interface PerformChange extends Function<String, Boolean> {
default PerformChange performChange(PerformChange input) {
System.out.println("1 this: " + this);
System.out.println("2 input: " + input);
return stringValue -> {
System.out.println("\n3 this: " + this);
System.out.println("4 input: " + input);
if (this.apply(stringValue)) {
System.out.println("5 this: " + this);
return input.apply(stringValue);
}
return false;
};
}
}
class PerformChangeImpl implements PerformChange {
private final String value;
public PerformChangeImpl(String value) {
this.value = value;
}
@Override
public Boolean apply(String string) {
System.out.println("\n6: " + this);
System.out.println("7: " + string);
return true;
}
@Override
public String toString() {
return "obj id: " + value;
}
}
和
private void executePerformChanges() {
System.out.println(performChangeList.stream()
.reduce(PerformChange::performChange)
.orElse(new PerformChangeImpl("4"))
.apply("test"));
}
结果
1 this: obj id: 1
2 input: obj id: 2
1 this: j8Test.PerformChange$$Lambda$2/284720968@2f4d3709
2 input: obj id: 3
3 this: j8Test.PerformChange$$Lambda$2/284720968@2f4d3709
4 input: obj id: 3
3 this: obj id: 1
4 input: obj id: 2
6: obj id: 1
7: test
5 this: obj id: 1
6: obj id: 2
7: test
5 this: j8Test.PerformChange$$Lambda$2/284720968@2f4d3709
6: obj id: 3
7: test
true
Process finished with exit code 0
【问题讨论】:
-
感谢您的快速接受 ;-)
标签: java lambda java-8 predicate functional-interface