2. Java 8: Using Built-in Lambda Types Flashcards

1
Q

Java 8 comes with some most-used functional interfaces, in which package are these interfaces hosted?

A

java.util.function

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

Name the functional interface that represents a boolean valued function of one argument that takes the type of T.

A

Predicate

public boolean test(T t)

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

Name the functional interface that represents a operation with no return value and accepts one argument of type T.

A

Consumer

public void accept(T t)

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

Name the functional interface that represents a function that returns a value of R and accepts one argument of type T.

A

Function

public R apply(T t);

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

Name the functional interface that is a supplier of values.

A

Supplier

public T get();

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

Name the functional interface that represents a function that produces a double result.

A

ToDoubleFunction

public double applyAsDouble(T t);

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

Name the functional interface that represents a boolean valued function of two arguments that takes the type of T and U.

A

BiPredicate

public boolean test(T t, U u);

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

Name the functional interface that represents a operation on a single argument that returns the same type as the argument.

A

UnaryOperator extends Function

public T apply(T t); // inherited from Function

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

Use the Function interface to create a Function that creates a new Boolean from a String. And then use that function to convert 3 strings to a Boolean:
true, false, dfa

A

Function function = s -> new Boolean(s);

s. apply(“true”); // true
s. apply(“false”); // false
s. apply(“dfa”); // false

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

Use the Consumer interface to loop over a String list and System.out their values. The list is calles values.

A

Consumer consumer = s -> System.out.println(s);
for(String value : values) {
s.accept(value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Given the following class:
class Book {
   String title;
   String author;
// getters ommited
   public Book(title, author) {}
}

How would one use the Supplier interface to create and return a new instance and print the title.

A
Supplier supplier = () -> new Book("title", "author");
System.out.println(supplier.get().getTitle());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
How could you rewrite the following code to use a constructor reference?
Supplier supplier = () -> new User();
A

Supplier supplier = User::new;

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

Use the UnaryOperator that takes a String and prints that with the addition of the string “ is Great!”.

A

UnaryOperator operator = s -> s + “ is Great!”;

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

Use the Predicate to print true when a String is larger then 5 characters.

A

Predicate predicate = s -> s.length() > 5;

System.out.println(predicate.test(“verylargestring”));

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

The usual FunctionalInterfaces only accept reference values as their parameters and return values. How can you use primitves in the FunctionalInterfaces so that no boxing and unboxing takes place?

A

Use the specialized interfaces for primitive types. For example given the Function interface there are additional interfaces for example:
IntFunction
IntToDoubleFunction

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

What are the four types of method references available?

A

Static Method Reference
Constructor Reference
Instance method reference of particular class type
Instance method reference of a object

17
Q

Use a IntFunction to convert a int to a String (valueOf method) use a static method reference.

A

IntFunction f1 = String::valueOf;

f1.apply(100);

18
Q

Use a Function to convert a StringBuilder to a String using the String constructor as a method refernce.

A
Function f1 = String::new;
f1.apply(new StringBuffer().append("test"));
19
Q

Use a BiFunction to compare two strings (equalsIgnoreCase) using a instance method reference of a particular class type.

A

BiFunction f1 = String::equalsIgnoreCase;
fi1.apply(“hello”, “HELLO”);

When using a instance method reference of particular type the first argument is used as the target for the method so it would be written like:
“hello”.equalsIgnoreCase(“HELLO”);

20
Q

Use a Supplier and a instance method reference of a object to convert a Integer to a String (toString);

A
Integer i = new Integer(1);
Supplier f1 = i::toString;
f1.get();