java-lambda

Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等。


import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Lambda 学习
 *
 * <pre>
 *     深入浅出 Java 8 Lambda 表达式  http://blog.oneapm.com/apm-tech/226.html
 *     关于Java Lambda表达式看这一篇就够了  https://objcoding.com/2019/03/04/lambda/
 *     Java 8 lambda 最佳实践 https://wizardforcel.gitbooks.io/java8-tutorials/content/Java%208%20lambda%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.html
 *
 * </pre>
 *
 * @author: weikeqin1@jd.com
 * @date: 2019-10-22 20:33
 **/
public class LambdaSample {


    @Test
    public void threadStartOld() {
        Worker w = new Worker();
        Thread t = new Thread(w);
        t.start();
    }

    @Test
    public void threadStartOld2() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello from thread");
            }
        }).start();
    }

    @Test
    public void threadStartNew() {
        new Thread(() -> System.out.println("abcd")).start();

        // Runnable r = () -> System.out.println("hello world");

    }

    public void test1() {
        Consumer<Integer> c = (x) -> {
            System.out.println(x);
        };

        BiConsumer<Integer, String> b = (Integer x, String y) -> System.out.println(x + " : " + y);

        Predicate<String> p = (String s) -> {
            return s == null;
        };
    }

}


class Worker implements Runnable {

    @Override
    public void run() {
        System.out.println("abcd");
    }

}
/**
 *
 */
@Test
public void arrCompareOld() {
    String[] strs = new String[]{"d", "c", "b", "a"};
    Arrays.stream(strs).forEach(System.out::println);

    Arrays.sort(strs, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    });
    Arrays.stream(strs).forEach(System.out::println);

}

/**
 *
 */
@Test
public void arrCompareNew() {
    String[] strs = new String[]{"d", "c", "b", "a"};
    Arrays.stream(strs).forEach(System.out::println);

    Arrays.sort(strs, (o1, o2) -> {
        //return o1.compareTo(o2);
        return Integer.compare(o1.length(), o2.length());
    });
    Arrays.stream(strs).forEach(System.out::println);
}

/**
 * 
 */
@Test
public void arrCompareNew2() {
    String[] strs = new String[]{"d", "c", "b", "a"};
    Arrays.stream(strs).forEach(System.out::println);

    //参数调用其类的相同方法,再简化
    Arrays.sort(strs, Comparator.comparingInt(String::length));
    Arrays.stream(strs).forEach(System.out::println);
}
/**
 * 方法的引用
 */
@Test
public void methodUse() {
    String[] strs = {"a", "b", "1", "2"};
    List<String> list = Arrays.asList(strs);
    list.forEach(x -> {
        System.out.println(x);
    });

    /**  :: 实际上面这对对象方法的引用 */
    List<String> strLen = list.stream().map(String::trim).collect(Collectors.toList());
    strLen.forEach(System.out::println);


    /** 方法的引用 */
    List<String> strLen2 = list.stream().map(s -> s.trim()).collect(Collectors.toList());
    strLen2.forEach(x -> {
        System.out.println(x);
    });
}
@Test
public void calOld() {
    //Old way:
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    for (Integer n : list) {
        int x = n * n;
        System.out.println(x);
    }
}

@Test
public void calNew() {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    list.stream().map((x) -> x * x).forEach(System.out::println);
}

References

[1] 深入浅出 Java 8 Lambda 表达式
[2] 关于Java Lambda表达式看这一篇就够了
[3] Java 8 lambda 最佳实践