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