The Java Functional Programming

 A, Lambda delay execution

  After some scenes of the code execution, the result will not necessarily be used, resulting in a waste of performance. The Lambda expressions are delayed execution, which can just
as solutions to enhance performance.

  Performance Logs waste case

    Note : log can help us quickly locate problems, document case the program is running in order to monitor and optimize the project.
    A typical scenario is that the parameters of conditions, for example after splicing log messages, in the printout condition is satisfied: 

 1 public class DemoLogger {
 2         private static void log(int level, String msg) {
 3             if (level == 1) {
 4                 System.out.println(msg);
 5             }
 6         }
 7 
 8         public static void main(String[] args) {
 9             String msgA = "Hello";
10             String msgB = "World";
11             String msgC = "Java";
12             log(1, msgA + msgB +msgC);
13          }
 14      }

 

  Problems code: regardless of the level meets the requirements as log second method parameters, will first three strings are spliced side and passed
the method will then perform the level determination. If the level is not met, then the splicing operation strings for nothing, there is a performance wasted.
  Lambda more preferably written
   using a Lambda necessarily requires a function interface:

1 @FunctionalInterface
2 public interface MessageBuilder {
3     String buildMessage();
4 }

 

  Then log transformation method

 1 public class Demo02LoggerLambda {
 2         private static void log(int level, MessageBuilder builder) {
 3             if (level == 1) {
 4                 System.out.println(builder.buildMessage());
 5             }
 6         }
 7 
 8         public static void main(String[] args) {
 9             String msgA = "Hello";
10             String msgB = "World";
11             String msgC = "Java";
12             log(1, () ‐ > msgA + msgB + msgC );
13         }
14     }

 

  As a result, only when the level to meet the requirements, will be spliced ​​with three strings; otherwise the three strings will not be spliced.

  Lambda prove delay

  Verified by the following codes:

 1 public class DemoLoggerDelay {
 2         private static void log(int level, MessageBuilder builder) {
 3             if (level == 1) {
 4                 System.out.println(builder.buildMessage());
 5             }
 6         }
 7 
 8         public static void main(String[] args) {
 9             String msgA = "Hello";
10             String msgB = "World";
11             String msgC = "Java";
12             log(2, () ‐ > {
13                     System.out.println("Lambda执行!");
14             return msgA + msgB + msgC;
15             });
16         }
17     }

 

  从结果中可以看出,在不符合级别要求的情况下,Lambda将不会执行。从而达到节省性能的效果。

  Tips:实际上使用内部类也可以达到同样的效果,只是将代码操作延迟到了另外一个对象当中通过调用方法来完成。而是否调用其所在方法是在条件判断之后才执行的。

二、使用Lambda 作为参数和返回值

    Java中的Lambda表达式可以被当作是匿名内部类的替代品。如果方法的参数是一个函数式接口类型,那么就可以使用Lambda表达式进行替代。

    使用Lambda表达式作为方法参数,其实就是使用函数式接口作为方法参数。

    例如 java.lang.Runnable 接口就是一个函数式接口,假设有一个 startThread 方法使用该接口作为参数,那么就可以使用Lambda进行传参。这种情况其 实和 Thread 类的构造方法参数为 Runnable 没有本质区别。

    Demo:

1 public class DemoRunnable {
2         private static void startThread(Runnable task) {
3             new Thread(task).start();
4         }
5 
6         public static void main(String[] args) {
7             startThread(() ‐ > System.out.println("线程任务执行!"));
8         }
9     }

 

    类似地,如果一个方法的返回值类型是一个函数式接口,那么就可以直接返回一个Lambda表达式。

    当需要通过一个方法来获取一个 java.util.Comparator 接口类型的对象作为排序器时,就可以调该方法获取。

    Demo:

 1  import java.util.Arrays;
 2     import java.util.Comparator;
 3 
 4     public class Demo06Comparator {
 5         private static Comparator<String> newComparator() {
 6             return (a,b) ‐>b.length() ‐a.length();
 7         }
 8 
 9         public static void main(String[] args) {
10             String[] array = {"abc", "ab", "abcd"};
11             System.out.println(Arrays.toString(array));
12             Arrays.sort(array, newComparator());
13             System.out.println(Arrays.toString(array));
14         }
15     }

 

    其中直接 return 一个Lambda 表达式即可。

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11614121.html