• 技术文章 >java >java基础

    java方法引用是什么

    小妮浅浅小妮浅浅2021-05-14 16:41:57原创7014

    1、说明

    方法引用可以看作是Lambda表达式的深层表达。换句话说,方法引用是Lambda表达式,也就是函数接口的例子,通过方法名称指向方法。

    2、使用场景

    当要传递给 Lambda 体的操作,已经实现的方法了,可以使用方法引用

    3、格式

    1

    类(或对象) :: 方法名

    4、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    public class MethodRefTest {

     

        // 情况一:对象 :: 实例方法

        //Consumer中的void accept(T t)

        //PrintStream中的void println(T t)

        @Test

        public void test1() {

            //使用Lambda表达

            Consumer<String> con1 = str -> System.out.println(str);

            con1.accept("中国");

            System.out.println("====================");

     

            //使用方法引用

            PrintStream ps = System.out;

            Consumer con2 = ps::println;

            con2.accept("China");

     

        }

     

        //Supplier中的T get()

        //Employee中的String getName()

        @Test

        public void test2() {

            //使用Lambda表达

            Employee emp = new Employee(1001, "Bruce", 34, 600);

            Supplier<String> sup1 = () -> emp.getName();

            System.out.println(sup1.get());

            System.out.println("====================");

     

            //使用方法引用

            Supplier sup2 = emp::getName;

            System.out.println(sup2.get());

     

     

        }

     

        // 情况二:类 :: 静态方法

        //Comparator中的int compare(T t1,T t2)

        //Integer中的int compare(T t1,T t2)

        @Test

        public void test3() {

            //使用Lambda表达

            Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1, t2);

            System.out.println(com1.compare(32, 45));

            System.out.println("====================");

     

            //使用方法引用

            Comparator<Integer> com2 = Integer::compareTo;

            System.out.println(com2.compare(43, 34));

        }

     

        //Function中的R apply(T t)

        //Math中的Long round(Double d)

        @Test

        public void test4() {

            //使用匿名内部类

            Function<Double, Long> func = new Function<Double, Long>() {

                @Override

                public Long apply(Double aDouble) {

                    return Math.round(aDouble);

                }

            };

            System.out.println(func.apply(10.5));

            System.out.println("====================");

     

            //使用Lambda表达式

            Function<Double, Long> func1 = d -> Math.round(d);

            System.out.println(func1.apply(12.3));

            System.out.println("====================");

     

            //使用方法引用

            Function<Double, Long> func2 = Math::round;

            System.out.println(func2.apply(12.6));

     

     

        }

     

        // 情况三:类 :: 实例方法

        // Comparator中的int comapre(T t1,T t2)

        // String中的int t1.compareTo(t2)

        @Test

        public void test5() {

            //使用Lambda表达式

            Comparator<String> com1 = (s1, s2) -> s1.compareTo(s2);

            System.out.println(com1.compare("abd", "aba"));

            System.out.println("====================");

     

            //使用方法引用

            Comparator<String> com2 = String::compareTo;

            System.out.println(com2.compare("abd", "abc"));

        }

     

        //BiPredicate中的boolean test(T t1, T t2);

        //String中的boolean t1.equals(t2)

        @Test

        public void test6() {

            //使用Lambda表达式

            BiPredicate<String, String> pre1 = (s1, s2) -> s1.equals(s2);

            System.out.println(pre1.test("abc", "abc"));

            System.out.println("====================");

     

            //使用方法引用

            BiPredicate<String, String> pre2 = String::equals;

            System.out.println(pre2.test("abc", "abd"));

     

        }

     

        // Function中的R apply(T t)

        // Employee中的String getName();

        @Test

        public void test7() {

            //使用Lambda表达式

            Employee employee = new Employee(1001, "Tom", 45, 10000);

     

            Function<Employee, String> func1 =e->e.getName();

            System.out.println(func1.apply(employee));

            System.out.println("====================");

     

            //使用方法引用

            Function<Employee,String>func2 = Employee::getName;

            System.out.println(func2.apply(employee));

        }

    }

    以上就是java方法引用的介绍,希望对大家有所帮助。更多Java学习指路:Java基础

    专题推荐:java方法引用
    上一篇:java如何自定义函数式接口 下一篇:java中Stream的使用流程及注意

    相关文章推荐

    • 构造器与java方法的比较分析• java方法重载• java方法重载的无效探究• java方法参数中通配符的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网