Java 8 - Lambda Expression By using Functional Interface
Lambda Expression By using Functional Interface:
interface inter{
public void m1();
}
Java 7:
Class Demo implementes Inter{
public void m1(){
// 100 lines of code here
System.out.println("m1 executed");
}
}
Class Test {
public static void main(String args[]){
Inter i = new Demo();
i.m1();
}
}
Java 8:
Case 1:
-------
Class Test1 {
public static void main(String args[]){
Inter i = ()-> {
// 100 lines of code here
System.out.println("m1 executed");
}
}
}
Example:
Java7
Class MyRunnable implements Runnable
{
public void run(){
for(int i=10;i<10;i++){
System.out.println("Child Thread");
}
}
}
Class ThreadDemo1{
public static void main(String args[]){
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
Java 8:
Class ThreadDemo1{
public static void main(String args[]){
Runnable r = () -> {
for(int i=0;i<10;i++){
System.out.println("Child Thread");
}
}
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
interface inter{
public void m1();
}
Java 7:
Class Demo implementes Inter{
public void m1(){
// 100 lines of code here
System.out.println("m1 executed");
}
}
Class Test {
public static void main(String args[]){
Inter i = new Demo();
i.m1();
}
}
Java 8:
Case 1:
-------
Class Test1 {
public static void main(String args[]){
Inter i = ()-> {
// 100 lines of code here
System.out.println("m1 executed");
}
}
}
Example:
Java7
Class MyRunnable implements Runnable
{
public void run(){
for(int i=10;i<10;i++){
System.out.println("Child Thread");
}
}
}
Class ThreadDemo1{
public static void main(String args[]){
Runnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
Java 8:
Class ThreadDemo1{
public static void main(String args[]){
Runnable r = () -> {
for(int i=0;i<10;i++){
System.out.println("Child Thread");
}
}
Thread t = new Thread(r);
t.start();
for(int i=0;i<10;i++){
System.out.println("Main Thread");
}
}
}
Comments
Post a Comment