Java基础:Lambda表达式

示例代码:

  1. interface MyValue{
  2.     double getValue();
  3. }
  4. interface MyParamValue{
  5.     double getValue(double v);
  6. }
  7. interface NumericTest{
  8.     boolean test(int n,int m);
  9. }
     
  10. public class lambdaDemo {
  11.     public static void main(String[] args) {
  12.         
  13.         MyValue myVal=()->98.6;    //lambda表达是符号
  14.         System.out.println(myVal.getValue());
  15.         
  16.         MyParamValue myPVal=(n)->1/n;
  17.         System.out.println(myPVal.getValue(4.0));
  18.         System.out.println(myPVal.getValue(8.0));
  19.  
  20.        NumericTest isFactor=(n,d)->(n%d)==0;
  21.        NumericTest lessThan=(n,m)->(n<m);
  22.        NumericTest absEqual=(n,m)->(n<0?-n:n)==(m<0?-m:m);
  23. -----------------------------------------------------------------------------------
  24. 块lambda表达式:
  25. NumericFunc smallestF=(n)->{
  26.             int result=1;
  27.             
  28.             n=n<0?-n:n;
  29.             
  30.             for(int i=2;i<=n/i;i++) {
  31.                 if(n%i==0) {
  32.                     result=i;
  33.                     break;
  34.                 }
  35.             }
  36.             return result;
  37.         };
  38.         System.out.println(smallestF.func(12));
  39.         System.out.println(smallestF.func(11));            
  40.     }
  41. }
  42. ----------------------------------------------------------------------
  43. 泛型函数式接口:
  44. interface SomeTest<T>{
  45.     boolean test(T n,T m);
  46.  
  47.         SomeTest<Integer> isFactor=(n,d)->(n%d)==0;
  48.         if(isFactor.test(10, 2)) {
  49.             System.out.println("2 is a factor of 10.");
  50.         }
  51.         System.out.println();
  52. ---------------------------------------------------------------
  53. lambda表达式抛出异常
  54. import java.io.*;
  55.  
  56. interface MyIOAction{
  57.     boolean ioAction(Reader rdr)throws IOException;  //抛出异常
  58. }
  59. interface MyTransform<T>{//使用数组作为形参
  60.     void transform(T[] a);
  61. }
  62. public class lambdaExceptionDemo {
  63.     public static void main(String[] args) {
  64.         
  65.         MyIOAction myIO=(rdr)->{
  66.             int ch=rdr.read();    //可能抛出异常
  67.             
  68.             return true;
  69.         };
  70.         System.out.println("---------------------");
  71.         
  72.         Double[] values= {1.0,2.0,3.0,4.0};
  73.         MyTransform<Double> sqrts=(v)->{
  74.             for(int i=0;i<v.length;i++) {
  75.                 v[i]=Math.sqrt(v[i]);
  76.                 System.out.println(v[i]);
  77.             }                        
  78.         };
  79.         sqrts.transform(values); //使用数组作为形参        
  80.     }
  81. }
  82. --------------------------------------------------------------------
  83. lambda表达式方法的引用   ClassName::MethdName
  84. interface IntPredicate{
  85.     boolean test(int n);
  86. }
  87. class MyIntPredicates{
  88.     private int v;
  89.     MyIntPredicates(int x){
  90.            v=x;
  91.     }
  92.     static boolean isPrime(int n) {
  93.         if(n<2) {
  94.             return false;
  95.         }
  96.         for(int i=2;i<=n/i;i++) {
  97.             if(n%i==0) {
  98.                 return false;
  99.             }
  100.         }       
  101.         return true;
  102.     }
  103.     
  104.     static boolean isEven(int n) {
  105.         return (n%2)==0;
  106.     }
  107.     
  108.     static boolean isPositive(int n) {
  109.         return n>0;
  110.     }
  111.     boolean isFactor(int n){
  112.          return (v%n)==0;
  113.    }
  114. }
  115. public class MethodRefDemo {
  116.     static boolean numTest(IntPredicate p,int v) {
  117.         return p.test(v);
  118.     }
  119.     
  120.     public static void main(String[] args) {
  121.  
  122.         boolean result;
  123.         result=numTest(MyIntPredicates::isPrime,17);
  124.         if(result) System.out.println("17 is Prime");
  125.         
  126.         result=numTest(MyIntPredicates::isEven,12);
  127.         if(result) System.out.println("12 is Even");
  128.         
  129.         result=numTest(MyIntPredicates::isPositive,11);
  130.         if(result) System.out.println("11 is Positive");
  131.     
  132.         MyIntPredicates myNum=new MyIntPredicates(12);
  133.         IntPredicate ip=myNum::isFactor;
  134.         result=ip.test(3);
  135.     }
  136. }
  137. ------------------------------------------------------------------
  138. 构造函数的引用:ClassName::new
  139. interface MyFuncs{
  140.     MyClass func(String s);   
  141. }
  142.  
  143. class MyClass{
  144.     private String str;
  145.     
  146.     MyClass(String s){
  147.         str=s;
  148.     }
  149.     
  150.     MyClass(){
  151.         str="";
  152.     }
  153.     
  154.     String getStr() {
  155.         return str;
  156.     }
  157. }
  158.         MyFuncs myClassCons=MyClass::new;       
  159.         MyClass mc=myClassCons.func("Test");     
  160.         System.out.println(mc.getStr());
  161.  

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87991579