Java Lambda Expression Constructor References Generic Class

Description

Java Lambda Expression Constructor References Generic Class


// Demonstrate a constructor reference with a generic class. 

// MyFunc is now a generic functional interface. 
interface MyFunc<T> {
  MyClass<T> func(T n);//from  w  w w.  j a v  a 2 s  .c  om
}

class MyClass<T> {
  private T val;

  MyClass(T v) {
    val = v;
  }

  MyClass() {
    val = null;
  }
  T getVal() {
    return val;
  };
}

public class Main {

  public static void main(String args[]) {
    MyFunc<Integer> myClassCons = MyClass<Integer>::new;

    MyClass<Integer> mc = myClassCons.func(100);

    System.out.println("val is " + mc.getVal());
  }
}



PreviousNext

Related