Java8 method reference and construction reference code instance

Time:2021-6-14

This article mainly introduces the java8 method reference and construction reference code examples. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

An overview of method reference

Method reference is a kind of abbreviation of specific lamda expression. The idea is to call function directly to use method name when lamda expression can be replaced.
Its syntax format: Class Name:: method name.

Two, three methods of reference

1 refers to a reference to a static method

Syntax format: static class name: method name

Example:

//1 lamda static method
  @Test
  public void LamdaSTest(){
    String youku1327 = "1327";
    Function function = s -> ObjectUtils.allNotNull(youku1327);
    System.out.println(function.apply(youku1327));// true

  }
  //Static method reference
  @Test
  public void MethodRefTest(){
    String youku1327 = "youku1327";
    Function function = ObjectUtils::allNotNull;
    System.out.println(function.apply(youku1327));// true
  }

2 reference to the method of the object instance

Syntax format: InstanceName: method name

This object refers to an external object that is not an input parameter

Example:

//2 lamda expression
  @Test
  public void ObjectLamdaTest(){
    Car car = new car ("100", "black", "China", 20) ";
    Supplier supplier = ()-> car.getColor();
    System.out.println(supplier.get());//black
  }
  //Object reference
  @Test
  public void ObjectRefTest(){
    Car car = new car ("100", "black", "China", 20) ";
    Supplier<String> supplier = car::getColor;
    System.out.println(supplier.get());//black
  }

three   Method reference to instance

Syntax format: object name (classname):: method name (method)
This object refers to the input object

//3 lamda expression
  @Test
  public void InstanceMethodLamdaTest(){
    Car car = new car ("100", "black", "China", 20) ";
    Function<Car,String> function = s -> s.getColor();
    System.out.println(function.apply(car));//black
  }

  @Test
  public void InstanceMethodRefTest(){
    Car car = new car ("100", "black", "China", 20) ";
    Function<Car,String> function = Car::getColor;
    System.out.println(function.apply(car));//black
  }

Triple constructor reference

Syntax format: object name (classname):: New


  @Test
  public void constructLamdaTest(){
    BiFunction<String,Double,Car> biFunction = (s, aDouble) -> new Car(s,aDouble);
    Car car = biFunction.apply("youku1327", 50.0);
    // Car(code=youku1327, color=null, factory=null, price=50.0)
    System.out.println(car);
  }

  @Test
  public void construcMethodRefTest(){
    BiFunction<String,Double,Car> biFunction = Car::new;
    Car car = biFunction.apply("youku1327", 50.0);
    // Car(code=youku1327, color=null, factory=null, price=50.0)
    System.out.println(car);
  }

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

[database] MySQL exercises and answers (educational administration management system)_ MySQL_ 5.7)

✨ statement The answer to the exercise is not guaranteed to be completely correct, which is for reference only MySQL:5.7 Recommended software: Navicat premium Learning SQL statements requires diligent practice! ✨ Field description of each table course CId Course serial number, primary key PCId Prerequisite course DId Opening department number, foreign key CName Course name […]