import java.util.*; import java.util.stream.Collectors; public class HigherOrder { public static void main(String[] args) { ArrayList numbers = new ArrayList(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); // the forEach function takes a lambda function and does it on all of the items numbers.forEach(n -> {System.out.println(n);}); System.out.println(); // the map function works on a "Stream" object, not a list directly // it also returns a stream, which can be forEach'd numbers.stream().map(n -> n * n).forEach(n -> {System.out.println(n);}); System.out.println(); // reduce is the same thing as fold int result = numbers.stream().reduce(0, (a, b) -> a + b); System.out.println("Sum = " + result); result = numbers.stream().reduce(1, (a, b) -> a * b); System.out.println("Product = " + result + "\n"); // filter exists on streams as well numbers.stream().filter(x -> x % 2 == 0).forEach(n -> {System.out.println(n);}); } }