Skip to content

Instantly share code, notes, and snippets.

@AshwinJay
Last active August 27, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshwinJay/a8150115fd56aed757386700be883f43 to your computer and use it in GitHub Desktop.
Save AshwinJay/a8150115fd56aed757386700be883f43 to your computer and use it in GitHub Desktop.
Simple Java lambda tests

Simple Java lambda tests

Some simple tests to satisfy my curiosity about what happens behind the scenes when you use lambdas.

Additionaly, you can use javap to see the code it generates: javap -l -v -p -c $YOUR_BUILD_DIR/com/foo/LambdaTests.class.

There are also some tests to understand Lambdas vs Closures and how it compare to other languages:

package com.foo;
import java.util.LinkedList;
import java.util.List;
public class LambdaCapture {
public static void main(String[] args) {
List<String> strings = new LinkedList<>();
strings.add("one");
strings.add("two");
strings.add("three");
List<Runnable> runners = new LinkedList<>();
strings
.forEach(s -> {
System.out.println(s);
runners.add(
() -> {
int i = runners.size();
System.out.println(s + " " + i);
}
);
});
runners
.forEach(
r -> r.run()
);
}
}
package com.foo;
import java.util.function.Supplier;
public class LambdaTests {
private int token;
private static String staticName(Object a) {
return a.toString() + " ABCD1234";
}
private String name(Object a) {
return a.toString() + " " + token;
}
private String current() {
return Integer.toString(token);
}
private void inc() {
token = token + 1;
}
private static void testPlain() {
final LambdaTests lambdaTests = new LambdaTests();
final String text = "Charlie Chaplin";
final Supplier<String> supplier1 = () -> staticName(text);
final Supplier<String> supplier2 = () -> lambdaTests.name(text);
lambdaTests.inc();
final Supplier<String> supplier3 = lambdaTests::current;
System.out.println(supplier1 + " " + supplier1.get());
System.out.println(supplier2 + " " + supplier2.get());
System.out.println(supplier3 + " " + supplier3.get());
}
private static void testWithObject(LambdaTests lambdaTests) {
final String text = "Laurel and Hardy";
final Supplier<String> supplier1 = () -> staticName(text);
final Supplier<String> supplier2 = () -> lambdaTests.name(text);
lambdaTests.inc();
final Supplier<String> supplier3 = lambdaTests::current;
System.out.println(supplier1 + " " + supplier1.get());
System.out.println(supplier2 + " " + supplier2.get());
System.out.println(supplier3 + " " + supplier3.get());
}
public static void main(String[] args) {
System.out.println("testPlain round 1");
testPlain();
System.out.println("testPlain round 2");
testPlain();
final LambdaTests lambdaTests = new LambdaTests();
System.out.println("testWithObject round 1");
testWithObject(lambdaTests);
lambdaTests.inc();
System.out.println("testWithObject round 2");
testWithObject(lambdaTests);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment