diff --git a/contents/textbook/lecture08/Streams/pipelineAndLaziness.md b/contents/textbook/lecture08/Streams/pipelineAndLaziness.md index 6c65b18b..e70b91cc 100644 --- a/contents/textbook/lecture08/Streams/pipelineAndLaziness.md +++ b/contents/textbook/lecture08/Streams/pipelineAndLaziness.md @@ -182,6 +182,79 @@ List integerList = intStream.boxed().collect(Collectors.toList()); ArrayList integerArrayList = new ArrayList<>(integerList); ``` +## Functional Interfaces that stream operations can take in + +### Predicate + filter (Predicate predicate) + java.util.function.Predicate + a simple function that takes a single value as parameter, and returns true or false + +```java +public interface Predicate { + boolean test(T t); +} +``` + +The Predicate interface contains more methods than the test() method, but the rest of the methods are default or static methods which you don't have to implement. +You can implement the Predicate interface using a class, like this: + +```java +public class CheckForNull implements Predicate { + @Override + public boolean test(Object o) { + return o != null; + } +} +``` + +You can also implement the Java Predicate interface using a Lambda expression. Here is an example of implementing the Predicate interface using a Java lambda expression: + +```java +Predicate predicate = (value) -> value != null; +``` + +### Consumer + forEach (Consumer consumer) + java.util.function.Consumer + a functional interface that represents a function that consumes a value without returning any value. + +```java +Consumer consumer = (value) -> System.out.println(value); +``` + +### Supplier + generate (Supplier s) + java.util.function.Supplier + a functional interface that represents a function that supplies a value of some sorts. + +```java +Supplier supplier = () -> new Integer((int) (Math.random() * 1000D)); +``` + +### Function + map (Function mapper) + java.util.function + a function (method) that takes a single parameter and returns a single value + + ```java +public interface Function { + + public apply(T parameter); +} +``` + +In the Function interface, the only method you have to implement is the apply() method. Here is a Function implementation example: + + ```java +public class IncreaseOne implements Function { + + @Override + public Integer apply(Integer a) { + return a + 1; + } +} +``` + ## Summary for Streams Here is a Summary for Streams that you can use during finals since there are so many methods for streams.