Skip to content

Instantly share code, notes, and snippets.

@btforsythe
Created February 15, 2017 23:09
Show Gist options
  • Save btforsythe/87d5c2e2d7caf7671fc4cfb47dae6e19 to your computer and use it in GitHub Desktop.
Save btforsythe/87d5c2e2d7caf7671fc4cfb47dae6e19 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class Demo {
public static void main(String[] args) {
Grill grill = new Grill();
Cooker broilReference = () -> grill.broil("Steak");
List<Cooker> cookers = Arrays.asList(broilReference, new Microwave()::nuke);
for (Cooker c : cookers) {
c.cook();
}
Function<String, String> cookAThing = (foo) -> grill.broil(foo);
cookAThing.apply("Shrimp");
cookAThing.apply("Zucchini");
}
// @FunctionalInterface
interface Cooker {
void cook();
}
static class Grill {
String broil(String food) {
System.out.println("broiled " + food + "!");
return food;
}
}
static class Microwave {
void nuke() {
System.out.println("nuked it!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment