The following code:
public static void main(String[] args) {
Collections.singleton(1).stream().forEach(i -> new Exception().printStackTrace());
}
prints:
java.lang.Exception
at PrintLambdaStackTrace.lambda$main$0(PrintLambdaStackTrace.java:6)
at PrintLambdaStackTrace$$Lambda$1/1831932724.accept(Unknown Source)
at java.util.Collections$2.tryAdvance(Collections.java:4717)
at java.util.Collections$2.forEachRemaining(Collections.java:4725)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at PrintLambdaStackTrace.main(PrintLambdaStackTrace.java:6)
How is the lambda invocation implemented? Why are there 2 stack frames?
PrintLambdaStackTrace$$Lambda$1/1831932724.accept(Unknown Source)
This is a generated class which implements the required interface. Its accept
method is just a stub, delegating to a method which was generated at compile time and added to the PrintLambdaStackTrace
class. This class is generated at lambda linkage time (the first time a lambda instance needs to be created).
PrintLambdaStackTrace.lambda$main$0(PrintLambdaStackTrace.java:6)
This is the method which actually implements lambda's behavior. It belongs to the PrintLambdaStackTrace
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With