Java Core: Lambdas (Functional Programming) Flashcards

1
Q

What are functional interfaces? How can you create a functional interface?

A

A functional interface is an interface that has only one unimplemented method. It may have other methods but they must not be abstract.

	A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces. 
	class Test {
	    public static void main(String args[])
	    {
	        // create anonymous inner class object
	        new Thread(new Runnable() {
	            @Override public void run()
	            {
	                System.out.println("New thread created");
	            }
	        }).start();
	    }
	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is ‘Optional’, and how do you work with it?

A

An Optional is a class that acts as a container for an object. And when Optional is initialized, it should specify the type it can contain (Optional, for example). Its purpose is to represent objects which may be null rather than having just a null reference.

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

What are streams?

A

A stream() turns a list into a sequence of elements that come one after the other. One or several stream operations can be used on each streamed element.

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

Describe the following stream operation - ‘filter’

A

▪ Filter - select only elements that mach a given predicate (condition);

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

Describe the following stream operation - ‘map’

A

Map - convert or change an object;

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

Describe the following stream operation - ‘min’/’max’

A

Min/max - get the minimum or maximum value from the streamed elements (a comparator can be used to determine how this is decided);

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

Describe the following stream operation - ‘find’/’findFirst’

A

▪ Find/findFirst - find the first element in the stream;

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

Describe the following stream operation - ‘collect’

A

Collect - packages streamed elements into a specified data structure (.collect(Collectors.toList());

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

Describe the following stream operation - ‘ifPresent’

A

ifPresent - performs an operation to a streamed Optional only if the contained object is not null.

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

When would you use lambdas?

A

Lambda expressions simplify development by making several parts of Java syntax optional, such as type declaration pf a parameter, return keyword, curl braces, etc. They should be used to pass functionality as an argument for methods.

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

Do you think lambdas (or functional approach in general) can be overused?

A

(Open-ended question, but the general goal is to demonstrate a scenario where at least some people would say that the functional approach becomes overused.)

The purpose of Lambdas is to simplify development. If a lambda makes a code less readable (harder to understand) then it defeats the purpose of using it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe the following stream operation - ‘forEach’

A

The forEach method is used to iterate through every element of the stream.

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