8. Lambda Expressions and Functional Interfaces Flashcards
What does the flatmap expects?
The flatmap expects a function that will take on element and create a Stream out of it
What does the mapToDouble method expects?
The mapToDouble method expects a ToDoubleFunction object. that will take an argument and return a double
Consumer
Represents an operation that accepts a single input argument and returns no result
public interface Consumer{ void accept(T t); }
Consumer consumer = s -> System.out.println(s);
Supplier
Represents an operation that does not take any argument and returns any kind of object aslong as it matches the type.
public interface Supplier{ T get(); }
Supplier supplier =
() -> “Hello!;
Function
represents an operation that takes any object and may return any kind of object of another type.
public interface Function{ R apply(T t); }
Function getName =
user -> user.getName();
Predicate
represents an operation that takes any kind of object, test it and returns a boolean
public interface Predicate{ boolean test(T t); }
Predicate isEmpty =
s -> s.isEmpty();
What does the forEach method expects?
The forEach() method expects a consumer as an argument
What does the map’s forEach method expects?
The map’s forEach method requires a bifurcation, a function that takes two arguments
Do you need parentheses by the parameters in a lambda?
you can leave off the parantheses around the parameter, however if you have
more than one parameter you must use the parentheses.
do you need curly braces when returning an expression?
If you try to return a single expression without curly braces, your lambda expression won’t compile. But you can use it only in curly braces.
Can you acces variables of the enclosing scope?
A lambda expression captures variables from the ecnlosing scope, so you can access those variables in the body of the lambda, but those variables must be final or effectively final. An effectively final variable is a variable or parameter whose value isn’t changed after it is initialized. You can’t modify the values of an variable in the lambda expression, otherwise you get an error.
What does the @FuncionalInterface do?
We can explicity define a functional interface using the @FunctionalInterface annotation. This
annotation is not required but can be helpful when you want to ensure you don’t inadvertently add methods to a functional interface that will then break other code.
What are the 4 basic types of functional interfaces?
Supplier, Consumer, Predicate and Function
What are the variations on supplier?
IntSupplier, DoubleSupplier, LongSupplier. The functional method names of each of
these is different and is not get(), the funtional method of IntSupplier is getAsInt().
What are the variations on consumer?
IntConsumer, DoubleConsumer, LongConsumer, their
accept() methods take one primitive argument and avoid the autoboxing you get with consumer.
what are variations on the primitive consumers?
there is ObjIntConsumer, ObjDoubleConsumer, ObjLongConsumer, whose
accept() methods take an object (type t) and a int , a double or a long.
What is a BiConsumer?
BiConsumer, its accept() method takes two object (types T and U, meaning the two objects don’t
have to be of the same type).
What do you need to remember with Lambdas?
you can’t modify a variable within the lambda, otherwise you will get a compiler error.
And outside the a lambda expression the variables must be final or effectively final.
Can we modify a field of an object?
If we want to use forEach() to iterate through a collection of objects to, we can’t return the value we find (because forEach() takes a consumer and the accept() of a consumer method is void), but we can change the field of an object from within the lambda to do the same thing.
What does the andThen() method do?
andThen() is actually a default method of the Consumer interface that you can use to chain
consumers together. Notice that the type of the consumer you pass to the
andThen() method must match the type of the consumer used as the first operation.
What default methods does the predicate interface have?
The default methods:
and(), or(), and negate(), are there so you can chain predicates together
What are the variations on predicate?
BiPredicate, DoublePredicate, IntPredicate, LongPredicate. BiPredicate’s test takes arguments and the others take one argument of a primitive type (to avoid
autoboxing).
What does the UnaryOperator do?
The one operator you should be familiar with for the exam is the UnaryOperator. It extends Function, so its functional method is also apply(). However it requires that the type of the argument to apply() be the same as the type of the return value.
What is method reference?
You can create method references for your own methods, too:
Trees.forEach(::);