1. package work.artorias.test.lambda;
    2. import java.util.Comparator;
    3. import java.util.function.Consumer;
    4. import java.util.function.Function;
    5. import java.util.function.Predicate;
    6. import java.util.function.Supplier;
    7. /**
    8. * 拉姆达表达式基础学习
    9. * @author LJ
    10. */
    11. public class LambdaBase {
    12. /**
    13. * 总结: 拉姆达表达式就是作为方法入参的一种简单书写形式,当一个方法入参是一个接口,并且该接口只有一个方法未实现,这种接口叫做函数式接口
    14. * ,就可以使用lambda表达式
    15. * lambda表达式的作用就是匿名实现了这个接口作为入参
    16. *
    17. * 为了方便我们使用JDK提供了许多函数式接口,其中有4中核心接口。以提供更方便的编程
    18. *
    19. */
    20. /**
    21. *
    22. * @param args
    23. */
    24. public static void main(String[] args) {
    25. System.out.println("1:");
    26. new LambdaBase().lambdaRule1();
    27. System.out.println("--------------------------------");
    28. System.out.println("2:");
    29. new LambdaBase().lambdaRule2();
    30. System.out.println("--------------------------------");
    31. System.out.println("3:");
    32. new LambdaBase().lambdaRule3();
    33. System.out.println("--------------------------------");
    34. System.out.println("4:");
    35. new LambdaBase().lambdaRule4();
    36. System.out.println("--------------------------------");
    37. System.out.println("5:");
    38. new LambdaBase().lambdaRule5();
    39. System.out.println("--------------------------------");
    40. }
    41. /**
    42. * 无参无返回值
    43. * 方法体只有一行可以省略{}
    44. */
    45. public void lambdaRule1(){
    46. LambdaTest1 lambdaTest1=()-> System.out.println("无参无返回值方法");
    47. lambdaTest1.a();
    48. }
    49. /**
    50. * 一个参数无返回值
    51. * 参数只有一个可以省略()
    52. *
    53. * Consumer<T> 接口是JDK提供用于使用lambda的接口,他是一个函数式接口,其中只有一个未实现方法。该接口也可以叫做消费者
    54. * void accept(T t);
    55. * 泛型标明了入参类型
    56. */
    57. public void lambdaRule2(){
    58. Consumer<String> stringConsumer=x->System.out.println(x);
    59. stringConsumer.accept("一个参数无返回值");
    60. }
    61. /**
    62. * 有多个参数,有返回值
    63. * Comparator<T>这是JDK自带的比较器接口,实现该接口的类需要实现一个方法以实现实例的比较,泛型T标明实例类型,
    64. * int compare(T o1, T o2);
    65. *
    66. * 这种就是通用写法,如果只有一条语句return也可以省略
    67. */
    68. public void lambdaRule3(){
    69. Comparator<Integer>integerComparator=(Integer x,Integer y)->{
    70. System.out.println("x="+x+" "+"y="+y);
    71. return Integer.compare(x,y);
    72. };
    73. int compare = integerComparator.compare(4, 5);
    74. System.out.println(compare);
    75. }
    76. /**
    77. 上面的补充,入参类型是可以省略的
    78. */
    79. public void lambdaRule4(){
    80. Comparator<Integer>integerComparator=(x, y)->{
    81. System.out.println("x="+x+" "+"y="+y);
    82. return Integer.compare(x,y);
    83. };
    84. int compare = integerComparator.compare(4, 5);
    85. System.out.println(compare);
    86. }
    87. /**
    88. * lambda中::的使用
    89. *
    90. * 基本就是如果有某个实现类的方法和你需要写的lambda一样,就可以直接使用
    91. * 就像类名可以直接调用静态方法一样,这里也可以
    92. *
    93. */
    94. public void lambdaRule5(){
    95. Consumer<String>stringConsumer=System.out::println;
    96. stringConsumer.accept("::的使用");
    97. }
    98. /**
    99. * jdk提供的四大函数式接口
    100. */
    101. public void functionInterfance(){
    102. /**
    103. *消费型接口
    104. * Consumer<T>
    105. * void accept(T t)
    106. */
    107. Consumer<String>stringConsumer=x-> System.out.println(x);
    108. /**
    109. *供给型接口
    110. * Supplier<T>
    111. * T get()
    112. */
    113. Supplier<String>stringSupplier=()->"供给型接口";
    114. /**
    115. *函数式接口
    116. * Function<T, R>
    117. * R apply(T t)
    118. */
    119. Function<String,Integer>stringIntegerFunction=x->Integer.parseInt(x);
    120. /**
    121. *断言式接口
    122. * Predicate<T>
    123. * boolean test(T t)
    124. */
    125. Predicate<Integer>integerPredicate=x->x.equals(0)?true:false;
    126. }
    127. interface LambdaTest1{
    128. void a();
    129. }
    130. }

    image.png