Skip to content

Instantly share code, notes, and snippets.

@santa4nt
Last active August 29, 2015 14:15
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 santa4nt/05baadcd5ed1c014a0de to your computer and use it in GitHub Desktop.
Save santa4nt/05baadcd5ed1c014a0de to your computer and use it in GitHub Desktop.
A sample usage of the lambda syntax in Java 8.
import java.util.function.Consumer;
public class SampleLambda {
// takes a function object that, in turn, takes a String argument and returns Void;
// this function object is a Consumer because it inherently returns nothing;
// the alternative--Function<T,R>--must have a return type
public static void foo(Consumer<String> func) {
func.accept("foo");
}
public static void main(String[] args) {
// using a variable-bound functor object
Consumer<String> functor = input -> {
System.out.println("lambda: " + input);
};
foo(functor);
// using lambda syntax to create an anonymous, inline functor object
foo( input -> { System.out.println("inline: " + input); } );
System.exit(0);
}
/* output:
*
* lambda: foo
* inline: foo
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment