Skip to content

Instantly share code, notes, and snippets.

@mrjink
Created November 13, 2021 19:27
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 mrjink/3527e9e1ff99166a099add8a8c6b28ce to your computer and use it in GitHub Desktop.
Save mrjink/3527e9e1ff99166a099add8a8c6b28ce to your computer and use it in GitHub Desktop.
Medium — java-8-lambdas — Step 1
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 = new AnimalMatcher() {
@Override
public boolean matches(Animal animal) {
return animal.canHop();
}
};
AnimalMatcher swimMatcher = new AnimalMatcher() {
@Override
public boolean matches(Animal animal) {
return animal.canSwim();
}
};
for (Animal animal : animals) {
if (hopMatcher.matches(animal)) {
System.out.println(animal.getName() + " can hop!");
}
if (swimMatcher.matches(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