Java 8 Method Reference

本文深入探讨Java8中方法引用的概念与应用,通过实例详细讲解静态方法引用、特定对象实例方法引用、任意类型对象实例方法引用及构造器方法引用的使用场景与语法。文章覆盖了如何将方法引用应用于函数式接口,简化代码并提高可读性。

Method references are a special form of the lambda expression.
You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.
Kinds of Method References

KindExample
Reference to a static methodContainingClass::staticMethodName
Reference to an instance method of a particular objectcontainingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular typeContainingType::methodName
Reference to a constructorClassName::new
1.Reference to a Static Method

Example1:

import java.util.function.Function;
 
public class Java8MethodReferences
{
    public static void main(String[] args) 
    {
        //Calling parseInt() method using lambda
         
        Function<String, Integer> lambdaFunction = (String s) -> Integer.parseInt(s);
        System.out.println(lambdaFunction.apply("12"));
         
        //Calling parseInt() method using method reference
         
        Function<String, Integer> referenceFunction = Integer::parseInt;
        System.out.println(referenceFunction.apply("12"));
    }
}

Example2:

import java.util.function.BiFunction;
class Multiplication{
    public static int multiply(int a, int b){
        return a*b;
    }
}
public class Test {
    public static void main(String[] args) {

        //Calling multiply() method using lambda
        BiFunction<Integer, Integer, Integer> lambdaProduct = (a,b) -> Multiplication.multiply(a,b);
        int lp = lambdaProduct.apply(12, 5);
        System.out.println("Product of given number is: "+lp);


        //Calling multiply() method using method reference
        BiFunction<Integer, Integer, Integer> referenceProduct = Multiplication::multiply;
        int rp = referenceProduct.apply(11, 5);
        System.out.println("Product of given number is: "+rp);
    }
}

Example3:

package com.boraji.tutorial;

import java.util.Arrays;
import java.util.List;

/**
 * @author imssbora
 */
class EvenOddChecker {
   public static boolean isEven(int n) {
      return n % 2 == 0;
   }
}

public class MethodReferenceExample1 {

   public static void main(String[] args) {
      List<Integer> numbers = Arrays.asList(20, 10, 15, 24, 55, 47, 16, 87, 88);

      // Print even numbers using lambda expression
      numbers.stream().map((n) -> EvenOddCheck.isEven(n))
          .forEach((n) -> System.out.println(n));
      
      // Print even numbers using method references
      numbers.stream().map(EvenOddCheck::isEven)
         .forEach(System.out::println);
   }

}
2.Reference to an instance method of a particular object

Example1:

import java.util.function.Supplier;
 
class Company
{
    String name;
     
    public Company(String name) 
    {
        this.name = name;
    }
     
    public String getName()
    {
        return name;
    }
}
 
public class Java8MethodReferences
{
    public static void main(String[] args) 
    {
        Company c = new Company("My_Company");
         
        //Calling getName() of c using lambda
         
        Supplier<String> lambdaSupplier = () -> c.getName();
        System.out.println(lambdaSupplier.get());
         
        //Calling getName() of c using method reference
         
        Supplier<String> referenceSupplier = c::getName;
        System.out.println(referenceSupplier.get());
    }
}

Example2:

@FunctionalInterface
interface MyInterface{
    void display();
}
public class Example {
    public void myMethod(){
        System.out.println("Instance Method");
    }
    public static void main(String[] args) {
        //Calling myMethod() method using lambda
        Example obj01 = new Example();  
        MyInterface ref01 = () -> obj01.myMethod();
        // Calling the method of functional interface
        ref01.display();


        //Calling myMethod() method using method reference
        Example obj02 = new Example();
        MyInterface ref02 = obj02::myMethod;
        // Calling the method of functional interface
        ref02.display();
    }
}

Example3:

package com.boraji.tutorial;

import java.util.function.BiFunction;

/**
 * @author imssbora
 */
class MathOperation {
   // Addition
   public int add(int a, int b) {
      return a + b;
   }
   // Subtraction
   public int sub(int a, int b) {
      return a + b;
   }
}

public class MethodReferenceExample2 {

   public static void main(String[] args) {

      MathOperation op = new MathOperation();

      System.out.println("--------------------Using lambda expression----------------------");
      BiFunction<Integer, Integer, Integer> add1 = (a, b) -> op.add(a, b);
      System.out.println("Addtion = " + add1.apply(4, 5));

      BiFunction<Integer, Integer, Integer> sub1 = (a, b) -> op.sub(a, b);
      System.out.println("Subtraction = " + sub1.apply(58, 5));

      System.out.println("---------------------Using method reference---------------------");
      BiFunction<Integer, Integer, Integer> add2 = op::add;
      System.out.println("Addtion = " + add2.apply(4, 5));

      BiFunction<Integer, Integer, Integer> sub2 = op::sub;
      System.out.println("Subtraction = " + sub2.apply(58, 5));
   }
}
3.Reference to an instance method of an arbitrary object of a particular type

Example1:

import java.util.function.Function;
 
public class Java8MethodReferences
{
    public static void main(String[] args) 
    {
        //Calling toLowerCase() method using lambda
         
        Function<String, String> lambdaFunction = (String s) -> s.toLowerCase();
        System.out.println(lambdaFunction.apply("JAVA"));
         
        //Calling toLowerCase() method using method reference
         
        Function<String, String> referenceFunction = String::toLowerCase;
        System.out.println(referenceFunction.apply("JAVA"));
    }
}

Example2:

import java.util.Arrays;
public class Example {

    public static void main(String[] args) {
        String[] stringArray = { "Steve", "Rick", "Aditya", "Negan", "Lucy", "Sansa", "Jon"};

        /* Lambda to an instance method of an arbitrary
         * object of a particular type
         */
        Arrays.sort(stringArray, (String a, String b) -> a.compareToIgnoreCase(b) );
        for(String str: stringArray){
            System.out.println(str);
        }


        /* Method reference to an instance method of an arbitrary
         * object of a particular type
         */
        Arrays.sort(stringArray, String::compareToIgnoreCase);
        for(String str: stringArray){
            System.out.println(str);
        }
        
    }
}

Example3:

package com.boraji.tutorial;

import java.util.ArrayList;
import java.util.List;

/**
 * @author imssbora
 */
public class MethodReferenceExample3 {

   public static void main(String[] args) {
      List<String> weeks = new ArrayList<>();
      weeks.add("Monday");
      weeks.add("Tuesday");
      weeks.add("Wednesday");
      weeks.add("Thursday");
      weeks.add("Friday");
      weeks.add("Saturday");
      weeks.add("Sunday");
      
      System.out.println("--------------Using lambda expression-----------------");
      weeks.stream().map((s)-> s.toUpperCase())
         .forEach((s)->System.out.println(s));
      
      System.out.println("--------------Using method reference-----------------");
      weeks.stream().map(String::toUpperCase)
      .forEach(System.out::println);
   }
}
4. Method reference to a constructor

Example1:

package com.boraji.tutorial;

import java.util.function.BiConsumer;

/**
 * @author imssbora
 */
class MathOperations {
   public MathOperations(int a, int b) {
      System.out.println("Sum of " + a + " and " + b + " is " + (a + b));
   }
}

public class MethodReferenceExample4 {

   public static void main(String[] args) {

      System.out.println("------------Using lambda expression------------");
      BiConsumer<Integer, Integer> addtion1 = (a, b) -> new MathOperations(a, b);
      addtion1.accept(10, 20);
      
      System.out.println("\n------------Using method reference------------");
      BiConsumer<Integer, Integer> addtion2 = MathOperations::new;
      addtion2.accept(50, 20);
   }
}

Example2:

@FunctionalInterface
interface MyInterface{
    Hello display(String say);
}
class Hello{
    public Hello(String say){
        System.out.print(say);
    }
}
public class Example {
    public static void main(String[] args) {
        //Lambda to a constructor
        MyInterface ref01 = (String say) -> new Hello(say);
        ref01.display("Hello World!");


        //Method reference to a constructor
        MyInterface ref02 = Hello::new;
        ref02.display("Hello World!");
    }
}

参考:
《Method References》
《Java 8 Method References》
《Method References in Java 8》
《Java 8 - Method references introduction with examples》

Java 中的 `Function` 接口和方法引用是函数式编程的重要组成部分,它们在功能和使用场景上存在明显区别。 ### Function 接口 `Function<T, R>` 是一个函数式接口,它定义了一个抽象方法 `R apply(T t)`,用于接受一个输入参数并返回一个结果。该接口支持函数式编程中的映射操作,例如将一个值转换为另一个值。`Function` 接口还提供了 `compose` 和 `andThen` 方法,允许组合多个函数以实现链式调用。 例如,以下代码展示了如何使用 `Function` 接口实现字符串到整数的转换: ```java Function<String, Integer> function = Integer::valueOf; Integer result = function.apply("123"); System.out.println(result); // 输出 123 ``` 此外,`Function` 接口的 `andThen` 方法可以将多个函数串联起来执行。例如,将字符串转换为整数后再进行平方运算: ```java Function<String, Integer> function1 = Integer::valueOf; Function<Integer, Integer> function2 = x -> x * x; Integer result = function1.andThen(function2).apply("123"); System.out.println(result); // 输出 15129 ``` ### 方法引用 方法引用是一种简化 Lambda 表达式的方式,它通过直接引用已有的方法来替代 Lambda 表达式。方法引用可以分为静态方法引用、实例方法引用、特定对象的实例方法引用和构造器引用。 例如,使用方法引用实现字符串到整数的转换可以简化为: ```java Function<String, Integer> function = Integer::valueOf; Integer result = function.apply("123"); System.out.println(result); // 输出 123 ``` 在构造器引用中,可以通过方法引用来调用类的构造函数。例如,创建一个 `Employee` 对象: ```java Function<String, Employee> fun = Employee::new; Employee employee = fun.apply("John"); ``` ### 区别总结 1. **定义与功能**: - `Function` 接口是一个函数式接口,主要用于实现单参数的映射逻辑。 - 方法引用是一种简化 Lambda 表达式的语法糖,通过引用已有方法来替代 Lambda 表达式[^1]。 2. **使用场景**: - `Function` 接口适用于需要将一个值转换为另一个值的场景。 - 方法引用适用于简化 Lambda 表达式的编写,特别是在已有方法可以直接使用的情况下[^2]。 3. **灵活性**: - `Function` 接口可以通过 `compose` 和 `andThen` 方法实现函数的组合与链式调用。 - 方法引用则主要用于替代 Lambda 表达式,提升代码的可读性和简洁性。 4. **构造器引用**: - `Function` 接口可以与构造器引用结合使用,实现对象的创建。 - 方法引用支持构造器引用,允许将构造器赋值给函数式接口,前提是构造器的参数列表与接口中的抽象方法一致[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值