Lambda Built in Func Int Flashcards

1
Q

Give a brief description of the functional interface Predicate{T}

A

Check a condition and returns a boolean value as result

Ex. filter() method in java.util.stream.Stream which is used to remove elements in the stream that dont match the given condition(predicate) as argument

boolean test(T t)

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

Give a brief description of the functional interface Consumer{T}

A

Operation that takes an argumenet but returns nothing

ex. forEach() method in collection and in java.util.stream.Stream; this method is used for transversing all the elements in the collection or stream

void accept(T t)

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

Give a brief description of the functional interface Function{T, R}

A

functions that take an argument and return a result

ex. In map() method, to transform or operate on the passed value and return a result

R apply(T t)

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

Give a brief description of the functional interface Supplier{T}

A

Operation that returns a value to the caller( the returned value could be same or different values)

generate() method in java.util.stream.Stream to create an infinite stream of elements

T get()

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

What is the difference between andThen() and compose() methods in Function interface?

A

The andThen() method applies the passed argumenet after calling the current Function. The compose() method calls the argument before calling the current function

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

What is constructor reference?

A

Using ::new

can be used with supplier

Supplier{String} newString = String::new;
System.out.println(newString.get());

or with function

Function{String, Integer} anotherInteger = Integer::new;
System.out.println(anotherInteger.apply(“100”));

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

Which of the following are valid lambda expressions?

  1. () -> “”
  2. x,y -> x+y
  3. (Coyote y) -> return 0
  4. (Camel c) -> { return}
  5. Wolf w -> 39
  6. () ->
  7. (Animal z, m) -> a
A

1 and 4 are valid

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

What does IntFunction receives as an argument?

A

takes a primitive argument but can return anything

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