216.
The above code snippet specifies
a lambda expression that
accepts a String value, converts it to uppercase and returns it.
So, it can be used to implement the following functional interface:
@FunctionalInterface
public interface Interface1 {
public String doSomething(String input);
}
This code defines a functional interface called Interface1. It
has a doSomething method that accepts as input a String and
returns a String value.
Explain how you can create a new Thread using a lambda
expression.
Answer:
The java.lang.Runnable interface is used to create a new
thread. Java 8 has designated this interface as a functional
interface and so you can implement it using a lambda expression.
The following code demonstrates this:
Runnable r = ()–> {
System.out.println(“Starting Thread..”);
//some more code
};
Thread myThread = new Thread(r);
myThread.start();
Here, the Runnable interface
is implemented via a lambda
expression that has a simple Sysout statement. A new Thread
object called myThread is created with this Runnable instance.