//Method Reference Example(Alternative Approach of Lambda Expression)
package methodconstructorreference;
public interface Interf
{
public void m1();
}
package methodconstructorreference;
public class Test {
public static void main(String[] args) {
//m1() implementation provide through lambda expression
Interf i=()->
{
System.out.println("lambda implementation");
};
i.m1();
}
}
o/p:-------------------------------------
lambda implementation
=======================================================================================================================
package methodconstructorreference;
public interface InterfMethodReference {
public void m1();
}
package methodconstructorreference;
public class TestMethodRef {
public static void m2()
{
System.out.println("method reference");
}
public static void main(String[] args) {
InterfMethodReference imr=TestMethodRef::m2;
imr.m1();
}
}
o/p:------------------------
method reference
=========================================================================================================================================
package methodconstructorreference;
public class TestRunnable {
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");
}
}
}
o/p:------------
mixed output
==========================================================================================================================================
package methodconstructorreference;
public class TestRunnableMethodReference {
public void m1()
{
for(int i=0;i<10;i++)
{
System.out.println("child Thread");
}
}
public static void main(String[] args) {
TestRunnableMethodReference trm=new TestRunnableMethodReference();
Runnable r=trm::m1;
Thread t=new Thread(r);
t.start();
for(int i=0;i<10;i++)
{
System.out.println("main Thread");
}
}
}
o/p:-------
mixed output
========================================================================
//Constructor Reference Example
package methodconstructorreference;
public class Sample {
Sample()
{
System.out.println("sample constructor execution and object creation");
}
}
package methodconstructorreference;
public interface ConstructorReferenceInterf {
public Sample get();
}
package methodconstructorreference;
public class ConstructorReferenceDemo {
public static void main(String[] args) {
//Through lambda expression
/* ConstructorReferenceInterf cri=()->
{
Sample s=new Sample();
return s;
};
Sample s1=cri.get();*/
//Through Constructor Reference
ConstructorReferenceInterf cri=Sample::new;
Sample s=cri.get();
}
}
o/p:---------------------
sample constructor execution and object creation
package methodconstructorreference;
public interface Interf
{
public void m1();
}
package methodconstructorreference;
public class Test {
public static void main(String[] args) {
//m1() implementation provide through lambda expression
Interf i=()->
{
System.out.println("lambda implementation");
};
i.m1();
}
}
o/p:-------------------------------------
lambda implementation
=======================================================================================================================
package methodconstructorreference;
public interface InterfMethodReference {
public void m1();
}
package methodconstructorreference;
public class TestMethodRef {
public static void m2()
{
System.out.println("method reference");
}
public static void main(String[] args) {
InterfMethodReference imr=TestMethodRef::m2;
imr.m1();
}
}
o/p:------------------------
method reference
=========================================================================================================================================
package methodconstructorreference;
public class TestRunnable {
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");
}
}
}
o/p:------------
mixed output
==========================================================================================================================================
package methodconstructorreference;
public class TestRunnableMethodReference {
public void m1()
{
for(int i=0;i<10;i++)
{
System.out.println("child Thread");
}
}
public static void main(String[] args) {
TestRunnableMethodReference trm=new TestRunnableMethodReference();
Runnable r=trm::m1;
Thread t=new Thread(r);
t.start();
for(int i=0;i<10;i++)
{
System.out.println("main Thread");
}
}
}
o/p:-------
mixed output
========================================================================
//Constructor Reference Example
package methodconstructorreference;
public class Sample {
Sample()
{
System.out.println("sample constructor execution and object creation");
}
}
package methodconstructorreference;
public interface ConstructorReferenceInterf {
public Sample get();
}
package methodconstructorreference;
public class ConstructorReferenceDemo {
public static void main(String[] args) {
//Through lambda expression
/* ConstructorReferenceInterf cri=()->
{
Sample s=new Sample();
return s;
};
Sample s1=cri.get();*/
//Through Constructor Reference
ConstructorReferenceInterf cri=Sample::new;
Sample s=cri.get();
}
}
o/p:---------------------
sample constructor execution and object creation
No comments:
Post a Comment