Java 8 Lambda

Java 8 Lambda

Miles Davenport

Filter, Map, Peek, and “::” method references
package com.chocksaway;

import java.util.*;
import java.util.stream.Collectors;

class Holder {
    private String holding;

    Holder(String holding) {
        this.holding = holding;
    }

    public String toString() {
        return this.holding;
    }
}



public class Main {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        Queue<String> queue = new LinkedList<>();

        stack.push("Apple");
        stack.push("pear");

        queue.add(stack.pop());
        queue.add("Miles");
        queue.add("Fruit");
        queue.add("John");

        List<String> filteredList = queue.stream().filter(each -> !each.equals("Miles"))
                .collect(Collectors.toList());

        List<Holder> holdingList = filteredList.stream()
                .peek(System.out::println) // peek for debuggin and lambda method reference
                .map(Holder::new) // lambda method reference
                .collect(Collectors.toList());

        holdingList.forEach(System.out::println); // nice way of using the ::println toString()
    }
}