Lambda expression, stream and functional interfaces are introduced in Java 8.

Lambda expressions provide a concise way to express instances of single-method interfaces (functional interfaces). A lambda expression is a block of code that gets passed around, like an anonymous method. It is a way to pass behaviour as an argument to a method invocation and to define a method without a name.

A stream is a sequence of data. It is a way to write code that is more declarative and less imperative
to process collections of objects.

A functional interface is an interface that contains one and only one abstract method. It is a way to define a contract for behaviour as an argument to a method invocation.

Below code shows creating a custom thread called Printer class by implementing Runnable interface and creating a thread using lambda.

Lambdas.java

public class Lambdas {
public static void main(String[] args) {
Thread thread = new Thread(new Printer());
thread.start();
Thread newThread = new Thread(() ->
System.out.println("Hello World2: "+ Thread.currentThread().getName()));
newThread.start();
}

}

class Printer implements Runnable {
public void run() {
System.out.println("Hello World1: " + Thread.currentThread().getName());
}
}

Comments

Popular posts from this blog

MongoDB

Git

JAXB