Last active
August 29, 2015 14:15
-
-
Save santa4nt/05baadcd5ed1c014a0de to your computer and use it in GitHub Desktop.
A sample usage of the lambda syntax in Java 8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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