Lambda: A Peek Under the Hood
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about how Java 8 lambdas and method references work under the hood, including invokedynamic, default methods and capture costs.
Recommended by 3 people across 6 different months, 2014 to 2014.
What it is
A one-hour Oracle session, presented by Brian Goetz, on how Java SE 8 lambda expressions are actually implemented. It explains that lambdas are not just shorthand for inner classes but are built on the invokedynamic feature from Java SE 7, and covers default methods and the cost of captured variables.
What people said
Strong rec fge · 2014-11 · Stack Overflow · on “How would I print the JVM's system properties using Java 8 and lambdas?”
One solution: public final class Foo { private static void printProperty(final Object key, final Object value) { System.out.println(key + ": " + value); } public static void main(final String... args) { System.getProperties().forEach(Foo::printProperty); } } Rundown: Properties extends Hashtable<Object, Object> which itself implements Map<Object, Object> ; Map has a .forEach() [LINK] method whose argument is a BiConsumer [LINK] ; BiConsumer is a functional interface [LINK] ; static method printProperty() of class Foo happens to have the same signature as a BiConsumer<Object, Object> : its "return value" is void , its first argument is Object , its second argument is Object ; we can therefore
Recommends fge · 2014-05 · Stack Overflow · on “Is there an aggregateBy method in the stream Java 8 api?”
By the way, if you are into this kind of stuff, Brian Goetz has also made this presentation [this video] ; very, very instructive.
Recommends Vinit Prajapati · 2014-06 · Stack Overflow · on “Preferences of abstract classes over interfaces in Java 8”
Regarding Point 1: In order to support lambda expression for all collection classes, like forEach method, it was necessary to add something which will have backward compatibility. see this video for detail Lambda Peak Under the hood [this video]
Recommends ngreen · 2014-09 · Stack Overflow · on “How to improve logging mechanism with Java8s lambdas”
A good starting point for discussion of capture costs in that video (skipping all the nice background info) would be around [36:25][1]. [1]: [this video]
Recommends fge · 2014-10 · Stack Overflow · on “Is it possible to convert method reference to MethodHandle?”
No you can't; recommended viewing: here [this video] . In short: method references etc are dealt with using invokedynamic call sites. Such call sites _are_ programmable however, see here [LINK] .
Recommends fge · 2014-12 · Stack Overflow · on “The costs of streams and closures in Java 8”
The cost you see here is not associated with the "closures" at all but with the cost of Stream initialization. Let's take your three sample codes: for (int i = 0; i < 10_000_000; i++) { Set<String> set = Collections.emptySet(); set.stream().forEach(s -> System.out.println(s)); } This one creates a new Stream instance at each loop; at least for the first 10k iterations, see below. After those 10k iterations, well, the JIT is probably smart enough to see that it's a no-op anyway. for (int i = 0; i < 10_000_000; i++) { Set<String> set = Collections.emptySet(); for (String s : set) { System.out.println(s); } } Here the JIT kicks in again: empty set? Well, that's a no-op, end of story. set.forEac
Cites McDowell · 2014-02 · Stack Overflow · on “How to improve logging mechanism with Java8s lambdas”
Maybe. You may incur a capture cost if you need to pass data from the calling method into `Callable.call`. This might be better than string concatenation but will still likely be more expensive than wrapping the `fine` call in an _if_ statement that checks if the level is enabled. Goetz discusses capture costs here [this video] .