Why Java 8 will introduce a lambda expression?

Why Java 8 will introduce lambda?

Before Java8 appear, if you want to pass to another piece of code in the method is very convenient. You will be almost impossible to pass around the block, because Java is an object-oriented language, so you have to build an object belongs to a class, by its methods to place a block of code that you want to pass.

Let's look at two very typical example, the thread comparator configured:

Thread structure:

When we want to execute some code logic in another thread, typically in the code to implement a method interface Runnable run which, as shown below:

public static void main(String[] args) {
    myThread t = new myThread();

}

class myThread implements Runnable {
    @Override
    public void run() {
        System.out.println("放入你想执行的代码");
    }
}

You write this code is to seek to open a new thread to execute your custom code to do this you create a myThread .

Then we look at construction comparator:

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    list.add(2);
    list.add(5);
    list.add(1);
    list.add(4);
    list.add(3);

    Comparator<Integer> comparator = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    };
    Collections.sort(list, comparator);
}

sort method will continue to call the compare method, the order of the elements does not reorder until ordered so far. You write the comparator is passed to the object sort method Collections of a code segment comparison logic, the code will be integrated into the Sort logic, this you wrote a class and a method .

These two examples you will see their similarities, passing a piece of code to other callers (a thread pool or a sorting method), this code will be called in the new method.

However, before Java8 appear, if you want to pass to another piece of code in the method is very convenient because Java is an object-oriented language, so you want to build a belongs to a class of objects by one of its a method to place you want to pass the code block. While in some other languages ​​may be directly transferred blocks of code, such as JS. To this end, Java decided to join the syntactic sugar lambda expression.

lambda transformation

For incoming comparator block, our aim is to compare the incoming logic section, the use of a lambda expression can be:

Lambda expression syntax is: Parameter -> an expression, if you want the incoming code is not represented by an expression, you can use the parameters -> {} a plurality of expressions. If no parameters need to be passed, it can be used () -> form of an expression.

public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(5);
        list.add(1);
        list.add(4);
        list.add(3);

        Collections.sort(list, (one,two)->{
            System.out.println("one="+one);
            System.out.println("two="+two);
            return two - one;
        });
    }

The compiler parses out one and two are the reference methods compare original interface, and automatically given type Integer.

For the construction of threads, our purpose is to pass the code for a thread of execution, the use of lambda expressions can do this:

public static void main(String[] args) {

    new Thread(() -> {
        for (int i = 0; i < 100; i++) {

            System.out.println("这是一个线程" + i);
        }
    }).start();

    for (int i = 100; i < 200; i++) {
        System.out.println("这是主线程" + i);
    }
}

From the above examples, with lambda expressions, much simpler transmission block, for certain usage scenario, e.g. Steam API, lambda your code will greatly improve the efficiency, and to be more than the conventional manner It is easy to read (of course, that you know the syntax for lambda expressions).

We still recommend the use of practical work, one might get the performance that matter, but you think lambda expression query performance loss than the cost of a small MySQL too much.

Guess you like

Origin www.cnblogs.com/keeya/p/11404631.html