Functional Programming Flashcards

1
Q

What is supplier?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the method used to get the valuse from Supplier

A

get();

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When does a supplier often used?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a string builder objects using supplier

A
Supplier stringBuilder = StringBuilder::new;
or
Supplier stringBuilder = () -> new StringBuilder();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you create a Supplier of ArrayList type?

A

Supplier> supplier = ArrayList::new;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consumer?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

BiConsumer?

A

That accepts two inputs but doesn’t return anything.

It two inputs doesn’t necessary to be same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When is a predicate often used?

A

Predicates are used for filtering/matching purposes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a BiPredicate?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

definition of Predicate and BiPredicate?

A
@FunctionalInterface
	public interface Predicate{
		boolean test(T t);
	}
	@FunctionalInterface
	public interface BiPredicate{
		boolean test(T t, U u);
	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the implications of the below statements?

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

A
  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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the method used for Function?

A

apply(T t) for Function

apply(T t, U u) for Bifunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the UNARYoPERATOR AND bINARYoPERATOR?

A

they are same as Functions except that they need the parameters of same type and return types also same type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the UNARYoPERATOR AND bINARYoPERATOR?

A

they are same as

17
Q

How to clear in deciding whether to pick predicate or function?

A

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
Q

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

A

Function should specify two generics and UnaryOperator should specify One generics.

BiFunction specify three generics and BinaryOperator specify one generics

19
Q

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

A

Function should specify two generics and UnaryOp

20
Q

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

A

Optional.isPresent()

21
Q

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

A

Optional.isEmpty()

22
Q

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

A

NoSuchElementException

23
Q

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

A

Optional.ofNullable(value)

24
Q

important Optional instance mthod to remember

A

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