Functional Programming Flashcards Preview

Java8 > Functional Programming > Flashcards

Flashcards in Functional Programming Deck (24)
Loading flashcards...
1

What is supplier?

Supplier is a functional interface that is used where we want to generate or supply values without taking any input

2

What is the method used to get the valuse from Supplier

get();

Eg.., Supplier date = LocalDate::now;
Localdate date = date.get();

3

When does a supplier often used?

A supplier is often used to when constructing a new objects.

4

How do you create a string builder objects using supplier

Supplier stringBuilder = StringBuilder::new;
or
Supplier stringBuilder = () -> new StringBuilder();

5

Can you create a Supplier of ArrayList type?

Supplier> supplier = ArrayList::new;

6

Consumer?

Consumers are used when we want to pass something but doesnt get anything in return.

7

BiConsumer?

That accepts two inputs but doesn't return anything.
It two inputs doesn't necessary to be same.

8

When is a predicate often used?

Predicates are used for filtering/matching purposes

9

What is a BiPredicate?

BiPredicate is same as Predicate except that it take two parameters instead of one

10

definition of Predicate and BiPredicate?

@FunctionalInterface
public interface Predicate{
boolean test(T t);
}

@FunctionalInterface
public interface BiPredicate{
boolean test(T t, U u);
}

11

what is the implications of the below statements?

BiPredicate bp1= String::startsWith;
BiPredicate bp2 = (String s1, String s2) -> s1.startsWith(s2);

1. startsWith() is an instance method.
2. Which means the first parameter in Bipredicate here is a instance variable on which the method is called

12

What method does the Predicate provides for && and ! respectively? What kind of method it is?

&& -> and();
! -> negate();
They are default methods of Predicates

13

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

14

What is the method used for Function?

apply(T t) for Function
apply(T t, U u) for Bifunction

15

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

16

What is the UNARYoPERATOR AND bINARYoPERATOR?

they are same as

17

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

18

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

19

What is a difference b/w Function and UnaryOperator apart fromtypes?

Function should specify two generics and UnaryOp

20

Method to check whether an Optional is having value or not.

Optional.isPresent()

21

Method to check whether an Optional is having value or not.

Optional.isEmpty()

22

What is the exception we get when there is no value in Optional and we try to get it?

NoSuchElementException

23

A method that wraps the value if Optional contains value or returns empty when optional is null?

Optional.ofNullable(value)

24

important Optional instance mthod to remember

get(), isPresent(), ifPresent(Consumer c), orElse(T t),orElseGet(Suppliers), oeElseThrow(Supplier s)