1. 1 package com.yhqtv.java1;
  2. 2
  3. 3 import org.junit.Test;
  4. 4
  5. 5 import java.util.ArrayList;
  6. 6 import java.util.Comparator;
  7. 7 import java.util.function.Consumer;
  8. 8
  9. 9 /*
  10. 10 * @author XMKJ yhqtv.com Email:yhqtv@qq.com
  11. 11 * @create 2020-05-11-14:28
  12. 12 *
  13. 13 * 总结六种情况:
  14. 14 ->左边:lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只一个参数,其一对()也可以省略
  15. 15 ->右边:lambda体应该使用一对{}包裹;如果lambda体只一条执行语句(可能是return语句,省略这一对{}和return关键字
  16. 16 *
  17. 17 *
  18. 18 *
  19. 19 */
  20. 20 public class LambdaTest1 {
  21. 21 //语法格式一:无参、无返回值
  22. 22 @Test
  23. 23 public void test1() {
  24. 24 Runnable r1 = new Runnable() {
  25. 25 @Override
  26. 26 public void run() {
  27. 27 System.out.println("我要坚持!!!");
  28. 28 }
  29. 29 };
  30. 30 r1.run();
  31. 31 System.out.println("=========================");
  32. 32
  33. 33 Runnable r2 = () -> System.out.println("我要坚持6666666!!!");
  34. 34
  35. 35
  36. 36 r2.run();
  37. 37 }
  38. 38
  39. 39 //语法格式二:Lambda 需要一个参数,但是没有返回值
  40. 40 @Test
  41. 41 public void test2() {
  42. 42 Consumer<String> con = new Consumer<String>() {
  43. 43 @Override
  44. 44 public void accept(String s) {
  45. 45 System.out.println(s);
  46. 46 }
  47. 47 };
  48. 48 con.accept("我要加油,加快速度");
  49. 49
  50. 50 System.out.println("=====================");
  51. 51
  52. 52 Consumer<String> con1 = (String s) -> System.out.println(s);
  53. 53
  54. 54 con1.accept("我一定要成功!");
  55. 55
  56. 56 }
  57. 57
  58. 58 //语法格式三:数据类型可以省略,因为可由编译器推断得出,称为“类型推断”
  59. 59 @Test
  60. 60 public void test3() {
  61. 61 Consumer<String> con1 = (String s) -> System.out.println(s);
  62. 62
  63. 63 con1.accept("我一定要成功!");
  64. 64
  65. 65 System.out.println("=====================");
  66. 66
  67. 67 Consumer<String> con2 = (s) -> System.out.println(s);
  68. 68
  69. 69 con1.accept("我一定要成功!666666");
  70. 70 }
  71. 71
  72. 72 @Test
  73. 73 public void test4() {
  74. 74 ArrayList<String> list = new ArrayList<>();//类型推断
  75. 75 int[] arr = {1, 2, 3};//类型推断
  76. 76 }
  77. 77
  78. 78 //语法格式四:Lambda 若只需要一个参数时,参数的小括号可以省略
  79. 79 @Test
  80. 80 public void test5() {
  81. 81 Consumer<String> con1 = (s) -> System.out.println(s);
  82. 82
  83. 83 con1.accept("我一定要成功!666666");
  84. 84
  85. 85 System.out.println("=====================");
  86. 86
  87. 87 Consumer<String> con2 = s -> System.out.println(s);
  88. 88
  89. 89 con2.accept("我一定要成功!666666");
  90. 90
  91. 91 }
  92. 92
  93. 93 //语法格式五:Lambda 需要两个或两个以上的参数,多条执行语句,并且可以有返回值
  94. 94 @Test
  95. 95 public void test6() {
  96. 96 Comparator<Integer> com1 = new Comparator<Integer>() {
  97. 97 @Override
  98. 98 public int compare(Integer o1, Integer o2) {
  99. 99 System.out.println(o1);
  100. 100 System.out.println(o2);
  101. 101 return o1.compareTo(o2);
  102. 102 }
  103. 103 };
  104. 104 System.out.println(com1.compare(12, 33));
  105. 105 ;
  106. 106
  107. 107 System.out.println("======================");
  108. 108
  109. 109 Comparator<Integer> com2 = (o1, o2) -> {
  110. 110 System.out.println(o1);
  111. 111 System.out.println(o2);
  112. 112 return o1.compareTo(o2);
  113. 113 };
  114. 114 System.out.println(com2.compare(12, 3));
  115. 115 ;
  116. 116
  117. 117 }
  118. 118
  119. 119 //语法格式六:当Lambda 体只有一条语句时,return 与大括号若有,都可以省略
  120. 120 @Test
  121. 121 public void test7() {
  122. 122
  123. 123 Comparator<Integer> com1 = (o1, o2) -> {
  124. 124 return o1.compareTo(o2);
  125. 125 };
  126. 126 System.out.println(com1.compare(12, 3));
  127. 127
  128. 128 System.out.println("======================");
  129. 129
  130. 130 Comparator<Integer> com2 = (o1, o2) -> o1.compareTo(o2);
  131. 131
  132. 132 System.out.println(com2.compare(12, 3));
  133. 133
  134. 134 }
  135. 135 }

 

版权声明:本文为yhqtv-com原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yhqtv-com/p/12869740.html