Created
November 13, 2021 22:49
-
-
Save mrjink/54902fa5d0591007703b6126c3da1b92 to your computer and use it in GitHub Desktop.
Medium — java-8-lambdas — Step 7
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.Predicate; | |
@FunctionalInterface | |
public interface AnimalMatcher extends Predicate<Animal> { | |
boolean test(Animal animal); | |
} |
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.Arrays; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
List<Animal> animals = Arrays.asList(new Frog(), new Kangaroo(), new Fish()); | |
AnimalMatcher hopMatcher = Animal::canHop; | |
AnimalMatcher swimMatcher = Animal::canSwim; | |
for (Animal animal : animals) { | |
if (hopMatcher.test(animal)) { | |
System.out.println(animal.getName() + " can hop!"); | |
} | |
if (swimMatcher.test(animal)) { | |
System.out.println(animal.getName() + " can swim!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment