Functional Programming Flashcards
What is supplier?
Supplier is a functional interface that is used where we want to generate or supply values without taking any input
What is the method used to get the valuse from Supplier
get();
Eg.., Supplier date = LocalDate::now;
Localdate date = date.get();
When does a supplier often used?
A supplier is often used to when constructing a new objects.
How do you create a string builder objects using supplier
Supplier stringBuilder = StringBuilder::new; or Supplier stringBuilder = () -> new StringBuilder();
Can you create a Supplier of ArrayList type?
Supplier> supplier = ArrayList::new;
Consumer?
Consumers are used when we want to pass something but doesnt get anything in return.
BiConsumer?
That accepts two inputs but doesn’t return anything.
It two inputs doesn’t necessary to be same.
When is a predicate often used?
Predicates are used for filtering/matching purposes
What is a BiPredicate?
BiPredicate is same as Predicate except that it take two parameters instead of one
definition of Predicate and BiPredicate?
@FunctionalInterface public interface Predicate{ boolean test(T t); }
@FunctionalInterface public interface BiPredicate{ boolean test(T t, U u); }
what is the implications of the below statements?
BiPredicate bp1= String::startsWith;
BiPredicate bp2 = (String s1, String s2) -> s1.startsWith(s2);
- startsWith() is an instance method.
2. Which means the first parameter in Bipredicate here is a instance variable on which the method is called
What method does the Predicate provides for && and ! respectively? What kind of method it is?
&& -> and();
! -> negate();
They are default methods of Predicates
What is a function?
A function takes a parameter of any type and returns a value of different type.
A Bifunction takes two parameter of any type and returning a value of any type.
The types doesnt need to be different
What is the method used for Function?
apply(T t) for Function
apply(T t, U u) for Bifunction
What is the UNARYoPERATOR AND bINARYoPERATOR?
they are same as Functions except that they need the parameters of same type and return types also same type
What is the UNARYoPERATOR AND bINARYoPERATOR?
they are same as
How to clear in deciding whether to pick predicate or function?
When you see a boolean returned, think Predicate unless the generics specify a Boolean return type.
Remeber, Predicate returns boolean return type, not Boolean object
What is a difference b/w Function and UnaryOperator apart fromtypes?
Function should specify two generics and UnaryOperator should specify One generics.
BiFunction specify three generics and BinaryOperator specify one generics
What is a difference b/w Function and UnaryOperator apart fromtypes?
Function should specify two generics and UnaryOp
Method to check whether an Optional is having value or not.
Optional.isPresent()
Method to check whether an Optional is having value or not.
Optional.isEmpty()
What is the exception we get when there is no value in Optional and we try to get it?
NoSuchElementException
A method that wraps the value if Optional contains value or returns empty when optional is null?
Optional.ofNullable(value)
important Optional instance mthod to remember
get(), isPresent(), ifPresent(Consumer c), orElse(T t),orElseGet(Suppliers), oeElseThrow(Supplier s)