Saturday, March 3, 2018

Method and Constructor references by using ::(double colon) operator

FunctionalInterface method can be mapped to our specified method by using :: (double colon) operator. This is called method reference.
Our specified method can be either static method or instance method.

FunctionalInterface method and our specified method should have same argument types, except this the remaining things like returntype, methodname, modifiers etc are not required to match.

syntax

if our specified method is static method.

Classname::methodName

if the method is instance method.

Objref::methodName

FunctionalInterface can refer lambda expression and FunctionalInterface can also refer method reference. Hence lambda expression can be replaced with method reference. Hence method reference is alternative syntax to lambda expression.
Ex: With Lambda Expression

class Test {
 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);
        }
  }
} 

With Method Reference

class Test {
 public static void m1() {
        for(int i=0; i<=10; i++) {
            System.out.println(Child Thread);
        }
 }
 public static void main(String[] args) {
        Runnable r = Test:: m1;
        Thread t = new Thread(r);
        t.start();
        for(int i=0; i<=10; i++) {
           System.out.println(Main Thread);
        }
 }
} 

In the above example Runnable interface run() method referring to Test class static method m1().

Method reference to Instance method:

Ex:

interface Interf {
    public void m1(int i);
}
class Test {
    public void m2(int i) {
         System.out.println(From Method Reference:+i);
    }
     public static void main(String[] args) {
             Interf f = I > sop(From Lambda Expression);
             f.m1(10);
             Test t = new Test();
             Interf i1 = t::m2;
             i1.m1(20);
     }
} 

In the above example functional interface method m1() referring to Test class instance method m2(). The main advantage of method reference is we can use already existing code to implement functional interfaces (code reusability).

Constructor References

We can use :: (double colon) operator to refer constructors also

Syntax: classname :: new

Ex:

Interf f = sample :: new;
functional interface f referring sample class constructor.  

Ex:

class Sample {
     private String s;
     Sample(String s) {
           this.s = s;
           System.out.println(Constructor Executed:+s);
     }
}
interface Interf {
      public Sample get(String s);
}
class Test {
      public static void main(String[] args) {
             Interf f = s > new Sample(s);
             f.get(From Lambda Expression);
             Interf f1 = Sample :: new;
             f1.get(From Constructor Reference);
      }
} 

Note

In method and constructor references compulsory the argument types must be matched.

Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: