java 8 review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Where do you define a lambda expression?

During the tutorial it was defined in the main method and then called using an instance variable of the functional interface

Supplier Functional Interface

Used in all context where there is no input but an output is expected. Has method T get() for this purpose

why is the supplier functional interface important for stream api

Used with filter method of stream api in the case that you want to return some information

functional interfaces

interface that can contain many static or default methods. however it can only have one abstract method

why is the predicate functional interface important for stream api

it is passed as an argument into filter method of stream api

why use an abstract method in a functional interface

they can be represented as an anonymous method using lambda expression

why use stream api

to process collections of objects

filter

used for conditional check in stream

Predicate Functional Interface

used for conditional check. Used when we can use true/false functions. It has method boolean test(T t) for this purpose.

for each

used for iteration in stream

why we need stream

we can achieve functional programming, reduce our code, and bulk operations have better performance

abstract method

A method that has no implementation.

default method in interface

A non-static method that has an implementation in an interface. Can be overriden, redeclared to make it abstract, or inherited.

lambda expression syntax

Calculator calculator = () -> {body}; where Calculator is an interface

what happens if you try to declare more than one abstract method in a functional interface?

Error occurs because only one abstract method is possible.

Do you need to pass data type of argument into lambda expression?

No

is a stream a data structure?

No it takes input from Collections, Arrays, or I/O channels. They also don't change the original data structure. They only provide the result as per the pipelined methods.

Can you pass a parameter to a lambda expression?

Yes as long as the abstract method in the functional interface takes a parameter.

How do you define a return type for a lambda expression?

You define the return type in the abstract method and then simply write the expression in the lambda expression. the return keyword is not necessary.

do you always need braces in a lambda expression definition

You do not need braces if you are writing just one line

what is a stream

a sequence of objects taht supports various methods which can be pipelined to produced the desired result

Consumer Functional Interface

built in functional interface. It can be used in all context where an object needs to be taken as input and some operation is to be performed without returning any result. Has abstract method void accept(T t) for this purpose.

what is the internal structure of for each method in stream api

consumer accept is called for each element of a collection. the accept does what ever it was defined to do.

Why is the consumer functional interface important for stream api

consumer is passed as an argument to for each method of stream api

static method in interface

contain the complete definition. these methods cannot be overridden or changed in the implementation class.


Ensembles d'études connexes

Principles of Leadership and Management

View Set

APUSH: Give Me Liberty Chapter 15 Notes (Reconstruction) (1865-1877)

View Set

Othello Test Review, Othello Act 3, Othello Act 2, Othello Act 1 (scenes: 1, 2, 3)

View Set