Java 8 in Action
Table of Contents
Chapter 1
Filtering Apples Outline
<<name the package>> <<imports>> public class FilteringApples { <<main>> <<filter green apples>> <<filter heavy apples>> <<is green apple?>> <<is heavy apple?>> <<generic apples filter>> <<Apple>> }
Apple Class
public static class Apple { <<Apple properties>> <<Apple constructor>> <<Apple weight getter>> <<Apple weight setter>> <<Apple color getter>> <<Apple color setter>> <<Apple pretty printer>> }
Properties
Filtering Methods
We're going to need build lists of Apple
s here, so we'll start with the empty
list as the result
:
List<Apple> result = new ArrayList<>();
so we can collect Apple
s matching some predicate:
result.add(apple);
Speaking of which, we can check if an Apple
is green:
"green".equals(apple.getColor())
or check if it's heavy, meaning its weight
is greater than 150
:
apple.getWeight() > 150
Filter Green Apples
public static List<Apple> filterGreenApples(List<Apple> inventory) { <<initialize result as the empty ArrayList>> <<collect the green apples iteratively>> return result; }
for (Apple apple: inventory) { if (<<green apple?>>) { <<add apple to result>> } }
Filter Heavy Apples
public static List<Apple> filterHeavyApples(List<Apple> inventory) { <<initialize result as the empty ArrayList>> <<collect the heavy apples iteratively>> return result; }
for (Apple apple: inventory) { if (<<heavy apple?>>) { <<add apple to result>> } }
Generic Apple Filter
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> pred) { <<initialize result as the empty ArrayList>> for (Apple apple: inventory) { if (pred.test(apple)) { <<add apple to result>> } } return result; }
Predicates
public static boolean isGreenApple(Apple apple) { return <<green apple?>>; }
public static boolean isHeavyApple(Apple apple) { return <<heavy apple?>>; }
Main
public static void main(String ... args) { <<inventory>> <<green apples by method reference>> <<heavy apples by method reference>> <<green apples via lambda>> <<heavy apples via lambda>> <<weird apples with overly complex lambda>> }
List<Apple> inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red"));
<<comment: just the green apples>> List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple); System.out.println(greenApples);
<<comment: just the heavy apples>> List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple); System.out.println(heavyApples);
<<comment: just the green apples>> List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> <<a green?>>); System.out.println(greenApples2);
<<comment: just the heavy apples>> List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> <<a heavy?>>); System.out.println(heavyApples2);
// [] List<Apple> weirdApples = filterApples(inventory, (Apple a) -> <<a light?>> || <<a brown?>>); System.out.println(weirdApples);
a.getWeight() > 150
a.getWeight() < 80
"green".equals(a.getColor())
"brown".equals(a.getColor())
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
// [Apple{color='green', weight=155}]
Name the package lambdasinaction.chap1
.
package lambdasinaction.chap1;
Import java.util.*
and Predicate<T>
.
import java.util.*; import java.util.function.Predicate;