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

An Apple has two properties, weight and color,

<<Apple weight>>
<<Apple color>>

where the weight is an int with the default value 0.

private int weight = 0;

and the color is a String with the default value "".

private String color = "";

Methods

Given a weight and a color, construct an Apple.

public Apple(int weight, String color) {
    this.weight = weight;
    this.color = color;
}
  • Getters

    Get the weight of an Apple.

    public Integer getWeight() {
        return weight;
    }
    

    Get the color of an Apple.

    public String getColor() {
        return color;
    }
    
  • Setters

    Set an Apple's weight.

    public void setWeight(Integer weight) {
        this.weight = weight;
    }
    

    Set an Apple's color.

    public void setColor(String color) {
        this.color = color;
    }
    
  • Pretty Printer
    public String toString() {
        return "Apple{" +
               "color='" + color + '\'' +
               ", weight=" + weight +
               '}';
    }
    

Filtering Methods

We're going to need build lists of Apples here, so we'll start with the empty list as the result:

List<Apple> result = new ArrayList<>();

so we can collect Apples 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;

Generated by Eric Bailey on 2017-03-20 Mon 06:35 using Emacs 25.1.1 (Org mode 9.0.5).