Created
February 15, 2017 23:09
-
-
Save btforsythe/87d5c2e2d7caf7671fc4cfb47dae6e19 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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