Functional Programming Flashcards

1
Q

What types of variables can lambda expressions use?

A

static variables
instance variables
effectively final variables in methods (effectively final meaning they are only ever assigned one value and never altered)

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

Functional Interface

Supplier

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 0
2) T
3) get

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

Functional Interface

Consumer

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 1 (T)
2) void
3) accept

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

Functional Interface

BiConsumer

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 2 (T, U)
2) void
3) accept

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

Functional Interface

Predicate

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 1 (T)
2) boolean
3) test

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

Functional Interface

BiPredicate

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 2 (T, U)
2) boolean
3) test

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

Functional Interface

Function

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 1 (T)
2) R
3) apply

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

Functional Interface

BiFunction

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 2 (T, U)
2) R
3) apply

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

Functional Interface

UnaryOperator

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 1 (T)
2) T`
3) apply

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

Functional Interface

BinaryOperator

1) # of Parameters?
2) Return Type?
3) Single Abstract Method It Contains?

A

1) 2 (T, T)
2) T`
3) apply

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

When do you use the Supplier functional interface?

A

When you want to generate or supply values without taking any input.

Think of it as creating something to supply

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

When do you use the Consumer or BiConsumer functional interface?

A

When you want to do something with a parameter (or two if BiConsumer) but not return anything.

Think of it as consuming the parameters

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

When do you use the Predicate or BiPredicate functional interface?

A

When filtering or matching.

It checks for a certain condition and returns true or false.

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

When do you use the Function or BiFunction functional interface?

A

When turning parameters into a value of a potentially different type and returning it.

Use the parameters to calculate/determine a result

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

When do you use the UnaryOperator or BinaryOperator functional interface?

A

When you want to transform a value (or two) into another value of the same type.

UnaryOperator extends Function

BinaryOperator extends BiFunction

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

Optional

A

A container object which may or may not contain a non-null value

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

How to represent an empty Optional object?

A

Optional.empty()

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

How to represent an Optional object with a value of 30?

A

Optional.of(30)

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

Optional.ofNullable(val)

A

Returns an Optional.empty() if val is null, or Optional.of(val) if val is non-null

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

Optional instance method

get()

A

When the Optional is empty, throws an exception

When the Optional contains a value, returns the value

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

Optional instance method

ifPresent(Consumer c)

A

When the Optional is empty, does nothing

When the Optional contains a value, calls Consumer c with the value

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

Optional instance method

isPresent()

A

When the Optional is empty, returns false

When the Optional contains a value, returns true

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

Optional instance method

orElse(T other)

A

When the Optional is empty, returns other

When the Optional contains a value, returns the value

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

Optional instance method

orElseGet(Supplier s)

A

When the Optional is empty, returns result of calling Supplier s

When the Optional contains a value, returns the value

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

Optional instance method

orElseThrow(Supplier s)

A

When the Optional is empty, throws the exception created by calling Supplier s

When the Optional contains a value, returns the value

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

Stream

A

A sequence of data

27
Q

Stream Pipeline

A

The operations that run on a stream to produce a result

It has a source which starts the stream, intermediate operations that produce a chain of streams, and the final operation that produces the result, known as the terminal operation.

28
Q

Stream package

A

java.util.stream

29
Q

How to create an empty Stream

A

Stream empty = Stream.empty();

30
Q

How to create a Stream with elements

A

Stream.of(T… t)

Can be one value, comma separated values, or an array

31
Q

How to create a Stream from a List

A

list.stream()

or

list.parallelStream(); // Allows the stream to process elements in parallel

32
Q

What does the following code do?

Stream.generate(Math::random)

A

Generates infinite random numbers

33
Q

What does the following code do?

Stream.iterate(1, n -> n + 2)

A

Generates infinite odd numbers

It starts with 1, then continues infinitely using the previous result as n in the lambda

34
Q

What is a reduction?

A

A special type of terminal operation where all of the contents of the stream are combined into a single primitive or Object.

35
Q

Terminal Operation

count()

A

Determines the number of elements in a finite stream.

Returns a long

36
Q

Terminal Operations

min() and max()

A

Allow you to pass a custom comparator and find the smallest or largest value in a finite stream according to that sort order.

Optional min(  super T> comparator) 
Optional max(  super T> comparator)
37
Q

Terminal Operations

findAny() and findFirst()

A

findAny() will return any element in the stream so long as it is not empty

findFirst() will return the first element in the stream so long as it is not empty

Works with infinite streams!

Optional findAny() 
Optional findFirst()
38
Q

Terminal Operations

anyMatch(), allMatch(), noneMatch()

A

anyMatch will find out whether at least one of the elements in the stream matches a given predicate.

allMatch will check every element in the stream and find out if it matches the predicate.

noneMatch will find if no elements in the stream match the specified predicate.

boolean anyMatch(Predicate super T> predicate) boolean allMatch(Predicate super T> predicate) boolean noneMatch(Predicate super T> predicate)

39
Q

Terminal Operation

forEach()

A

forEach will perform an action for each element in the stream. It is a simplified inline way to write a for loop.

void forEach(Consumer super T> action)

40
Q

Terminal Operation

reduce()

A

reduce uses a BinaryOperator to combine the elements of the stream into a single object.

Stream stream = Stream.of(3, 5, 6); System.out.println(stream.reduce(1, (a, b) -> a*b)); //90

T reduce(T identity, BinaryOperator accumulator) Optional reduce(BinaryOperator accumulator)
<u> U reduce(U identity, BiFunction<u> accumulator, BinaryOperator<u> combiner)</u></u></u>
41
Q

Terminal Operation

collect()

A

collect will convert the stream into some other container

Stream stream = Stream.of(“w”, “o”, “l”, “f”); StringBuilder word = stream.collect(StringBuilder::new,
StringBuilder::append, StringBuilder:append)

In this example, StringBuilder::new creates the object we will be adding elements of the stream to, the first StringBuilder::append tells it how to handle adding one element, and the second StringBuilder::append tells it how to handle combining two containers together (however it chooses to do that in the process)

 R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner)
 R collect(Collector super T, A,R> collector)

You can also use a predefined Collector such as this:

Stream stream = Stream.of(“w”, “o”, “l”, “f”); Set set = stream.collect(Collectors.toSet());

42
Q

Intermediate Operation

filter()

A

Returns a Stream with elements that match a given expression

Stream filter(Predicate super T> predicate)

Stream s = Stream.of(“monkey”, “gorilla”, “bonobo”);
s.filter(x -> x.startsWith(“m”)).forEach(System.out::print); // monkey

43
Q

Intermediate Operation

distinct()

A

Returns a stream with duplicate values removed

Stream distinct()

Stream s = Stream.of(“duck”, “duck”, “duck”, “goose”); s.distinct().forEach(System.out::print); // duckgoose

44
Q

Intermediate Operations

limit() and skip()

A

limit(n) specifies the max size of the stream is n

skip(n) will skip to the nth element in the stream

Stream limit(int maxSize)
Stream skip(int n)

Stream s = Stream.iterate(1, n -> n + 1); s.skip(5).limit(2).forEach(System.out::print); // 67

45
Q

Intermediate Operation

map()

A

map will transform the elements elements in a stream using the provided Function

Stream map(Function super T, ? extends R> mapper)

Stream s = Stream.of(“monkey”, “gorilla”, “bonobo”); s.map(String::length).forEach(System.out::print); // 676

46
Q

Intermediate Operation

flatMap()

A

Takes each element in the stream and makes any elements it contains top-level elements in a single stream.

This is helpful when you want to remove empty elements from a stream or you want to combine a stream of lists.

List zero = Arrays.asList();
List one = Arrays.asList(“Bonobo”);
List two = Arrays.asList(“Mama Gorilla”, “Baby Gorilla”); Stream> animals = Stream.of(zero, one, two);
animals.flatMap(l -> l.stream()).forEach(System.out::println);

47
Q

Intermediate Operation

sorted()

A

Returns a stream with the elements sorted

Stream sorted()
Stream sorted(Comparator super T> comparator)
48
Q

Intermediate Operation

peek()

A

Allows you to perform an operation on the current element of the stream

Stream peek(Consumer super T> action)

Stream s = Stream.of(“monkey”, “gorilla”, “bonobo”);
s.peek(System.out::println);

49
Q

What is the result of this code?

Stream.generate(() -> "Elsa") 
.filter(n -> n.length() == 4) 
.sorted()
.limit(2) 
.forEach(System.out::println);
A

It hangs until you kill it.

Sorted will wait to sort until filter is done passing it stuff, so it never reaches the limit(2)

50
Q

What is the result of this code?

Stream.generate(() -> "Elsa") 
.filter(n -> n.length() == 4) 
.limit(2)
.sorted() 
.forEach(System.out::println);
A

Elsa Elsa

When filter passes 2 elements through to sorted(), limit(2) tells the code to move on and do the sort.

51
Q

What is the result of this code?

Stream infinite = Stream.iterate(1, x -> x + 1); infinite.filter(x -> x % 2 == 1)
.peek(System.out::print)
.limit(5)
.forEach(System.out::print);

A

1133557799

The elements of the stream get passed through all the way down the pipeline before the next one

52
Q

IntStream

A

A stream that contains primitives.

Can be used for int, short, byte or char

53
Q

LongStream

A

A stream that contains primitives.

Can be used for long

54
Q

DoubleStream

A

A stream that contains primitives.

Can be used for double or float

55
Q

How to get the average of the numbers in this stream?

IntStream intStream = IntStream.of(1, 2, 3);

A

intStream.average()

Returns an OptionalDouble

56
Q

How to create an IntStream with the numbers in the range 1-5?

A

IntStream range = IntStream.range(1,6)

IntStream range = IntStream.rangeClosed(1,5)

57
Q

How to get the sum of the elements in this stream?

IntStream intStream = IntStream.of(1, 2, 3);

A

intStream.sum()

Returns an int

58
Q

How to find the largest and smallest element in this stream?

IntStream intStream = IntStream.of(1, 2, 3);

A

intStream.max()

intStream.min()

Returns an OptionalInt

59
Q

How to retrieve the value of the following OptionalInt?

OptionalInt optional;

A

optional.getAsInt()

If no element present, throws NoSuchElementException

60
Q

How to get info on an IntStream that includes the minimum, maximum, average, size, and the number of values in the stream.

A

IntSummaryStatistics stats = intStream.summaryStatistics();

getAverage()
getCount()
getMax()
getMin()
getSum()
61
Q

BooleanSupplier

A

Functional interface for boolean

boolean getAsBoolean()

62
Q

What is the output of the following code?

Stream ohMy = Stream.of(“lions”, “tigers”, “bears”); String result = ohMy.collect(Collectors.joining(“, “)); System.out.println(result);

A

lions, tigers, bears

63
Q

What is the output of the following code?

Stream ohMy = Stream.of(“lion”, “tigers”, “bears”);
Double result = ohMy.collect(Collectors.averagingInt(String::length)); System.out.println(result);

A

5.0

a Double