java8之lambda表达式入门

1.基本介绍
lambda表达式,即带有参数的表达式,为了更清晰地理解lambda表达式,先上代码:
1.1 两种方式的对比
1.1.1 方式1-匿名内部类
class Student{
    private String name;
    private Double score;
 
    public Student(String name, Double score) {
        this.name = name;
        this.score = score;
    }
 
    public String getName() {
        return name;
    }
 
    public Double getScore() {
        return score;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setScore(Double score) {
        this.score = score;
    }
 
    @Override
    public String toString() {
        return "{"
                + "\"name\":\"" + name + "\""
                + ", \"score\":\"" + score + "\""
                + "}";
    }
}:
@Test
public void test1(){
    List<Student> studentList = new ArrayList<Student>(){
        {
            add(new Student("stu1",100.0));
            add(new Student("stu2",97.0));
            add(new Student("stu3",96.0));
            add(new Student("stu4",95.0));
        }
    };
    Collections.sort(studentList, new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return Double.compare(o1.getScore(),o2.getScore());
        }
    });
    System.out.println(studentList);
}

代码调用Collections.sort方法对集合进行排序,其中第二个参数是一个匿名内部类,sort方法调用内部类中的compare方法对list进行位置交换,由于java中的参数类型只能是类或者基本数据类型,因此虽然传入的是一个Comparator类,可是实际上能够理解成为了传递compare方法而不得不传递一个Comparator类 ,这种方式显得比较笨拙,并且大量使用的话代码严重冗余,这种状况在java8中经过使用lambda表达式来解决。java

lambda表达式专门针对只有一个方法的接口(即函数式接口),Comparator就是一个函数式接口
@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
}
@FunctionalInterface的做用就是标识一个接口为函数式接口,此时Comparator里只能有一个抽象方法,由编译器进行断定。
使用lambda表达式以后方式1 中的代码改造以下
 
1.1.2 方式2-lambda表达式
public void test1_(){
        List<Student> studentList = new ArrayList<Student>(){
            {
                add(new Student("stu1",100.0));
                add(new Student("stu2",97.0));
                add(new Student("stu3",96.0));
                add(new Student("stu4",95.0));
            }
        };
        Collections.sort(studentList,(s1,s2)-> Double.compare(s1.getScore(),s2.getScore()));
        System.out.println(studentList);
    }
1.2 lambda语法
1.2.1 多参数
     (1). lambda表达式的基本格式为(x1,x2)->{表达式...};
     (2). 在上式中,lambda表达式带有两个参数,此时参数类型能够省略,但两边的括号不能省略
     (3). 若是表达式只有一行,那么表达式两边的花括号能够省略
1.2.2 无参数
一个常见的例子是新建一个线程,不使用lambda表达式的写法为
public void testThread(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello, i am thread!");
            }
        }).start();
    }
其中Runnable接口也是一个函数式接口,源码以下
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
将其转换为lambda表达式的写法为
public void testThread_(){
    new Thread(()-> System.out.println("hello, i am thread!")).start();
}
对于没有参数的状况 :
     (1).参数的括号不能省略,
     (2).其余语法同多参数
1.2.3 一个参数
咱们构造一个只有一个参数的函数式接口
@FunctionalInterface
public interface MyFunctionalInterface {
    public void single(String msg);
}
 
/**
 * 须要单个参数
 */
public static void testOnePar(MyFunctionalInterface myFunctionalInterface){
    myFunctionalInterface.single("msg");
}
/**
     * 一个参数,能够省略参数的括号
     */
    @Test
    public void testOneParameter(){
        testOnePar(x-> System.out.println(x));
    }
对于一个参数的状况:
     (1).能够省略参数的括号和类型
     (2).其余语法同多参数
1.3 jdk提供的经常使用函数式接口
在这里咱们为了演示只有一个参数的状况本身建立了一个函数式接口,其实java8中已经为咱们提供了不少常见的函数式接口,截图以下:
常见的有
Function:提供任意一种类型的参数,返回另一个任意类型返回值。 R apply(T t);
Consumer:提供任意一种类型的参数,返回空值。 void accept(T t);
Supplier:参数为空,获得任意一种类型的返回值。T get();
Predicate:提供任意一种类型的参数,返回boolean返回值。boolean test(T t);
所以针对上面的状况,咱们能够直接使用Consumer类,
/**
     * 须要单个参数
     */
    public static void testOnePar1(Consumer unaryOperator){
        unaryOperator.accept("msg");
    }
2.方法引用
lambda表达式用于替换函数式接口,方法引用也是如此,方法引用可使代码更加简单和便捷
2.1 小试牛刀
上代码,根据List中字符串长度排序:
public static void test1_() {
    List<String> strLst = new ArrayList<String>() {
        {
            add("adfkjsdkfjdskjfkds");
            add("asdfasdfafgfgf");
            add("public static void main");
        }
    };
    Collections.sort(strLst, String::compareToIgnoreCase);
    System.out.println(strLst);
}
只要方法的参数和返回值类型与函数式接口中抽象方法的参数和返回值类型一致,就可使用方法引用。
2.2 使用方式
方法引用主要有以下三种使用状况
     (1). 类::实例方法
     (2). 类::静态方法
     (3). 对象::实例方法
其中后两种状况等同于提供方法参数的lambda表达式,
如System.out::println 等同于(x)->System.out.println(x),
   Math::pow 等同于(x,y)->Math.pow(x,y).
第一种中,第一个参数会成为执行方法的对象,String::compareToIgnoreCase)等同于(x,y)->x.compareToIgnoreCase(y)
此外,方法引用还可使用this::methodName及super::methodName表示该对象或者其父类对象中的方法
class Father {
    public void greet() {
        System.out.println("Hello, i am function in father!");
    }
}
 
class Child extends Father {
    @Override
    public void greet() {
        Runnable runnable = super::greet;
        new Thread(runnable).start();
    }
}
public static void main(String[] args){
        new Child().greet();
    }
最后打印的结果为:Hello, i am function in father!
3.构造器引用
构造器引用同方法引用相似,一样做用于函数式接口
构造器引用的语法为 ClassName::new
啥也不说,线上代码
List<String> labels = Arrays.asList("aaa","bbb","ccc","ddd");
Stream<Button> buttonStream = labels.stream().map(Button::new);
如上代码所示,map方法内须要一个Function对象
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
调用Button的构造器,接收一个String类型的参数,返回一个Button类型的对象
public class Button extends ButtonBase {
 
        /**
         * Creates a button with the specified text as its label.
         *
         * @param text A text string for its label.
         */
        public Button(String text) {
            super(text);
            initialize();
        }
    }
另一个例子以下
Button[] buttons1 = buttonStream.toArray(Button[]::new);
toArray方法的申明以下
<A> A[] toArray(IntFunction<A[]> generator);
接收一个IntFunction类型的接口R apply(int value);该接口接收一个int型参数,返回指定类型
调用数组的初始化方法恰好适合。
有一个简单的构造器引用的例子以下:
public class LambdaTest3 {
 
    @Test
    public void test1_(){
        List<Integer> list = this.asList(ArrayList::new ,1,2,3,4,5);
        list.forEach(System.out::println);
    }
 
    public  <T> List<T> asList(MyCrator<List<T>> creator,T... a){
        List<T> list =  creator.create();
        for (T t : a)
            list.add(t);
        return list;
    }
}
interface MyCrator<T extends List<?>>{
    T create();
}
咱们在项目中常用asList来建立一个ArrayList,可是也只能是ArrayList,
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}
咱们如何在asList中指定建立哪一种类型的List的实例呢,使用构造器引用使得asList方法能够指定生成的List类型。
4.自由变量的做用范围
啥都不说,上代码先:
public class LambdaTest4 {
    public void doWork1(){
        Runnable runnable = ()->{
            System.out.println(this.toString());
            System.out.println("lambda express run...");
        };
        new Thread(runnable).start();
    }
 
    public void doWork2(){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(this.toString());
                System.out.println("anony function run...");
            }
        };
        new Thread(runnable).start();
    }
    public static void main(String[] args) {
        new LambdaTest4().doWork1();
        new LambdaTest4().doWork2();
    }
}
代码中doWork1和doWork2分别使用lambda表达式和匿名内部类的方式实现了Runnable接口,最后打印的结果以下
com.java8.lambda.LambdaTest4@74f84cf
lambda express run...
com.java8.lambda.LambdaTest4$1@4295c176
anony function run...
可见使用lambda表达式的方式,表达式中的this指的是包含lambda表达式的类,而使用匿名内部类的方式,this指的是匿名内部类自己。
4.1 自由变量和闭包
lambda达式中的变量有几类,1.参数内的变量,2.lambda表达式中的内部变量,3.自由变量,自由变量指的是在lambda表达式以外定义的变量。
包含自由变量的代码则称为闭包,若是理解了lambda表达式会在编译阶段被转换为匿名内部类,那么能够很容易理解自由变量在lambda表达式中的做用范围,在lambda表达式中会捕获全部的自由变量,而且将变量定义为final类型,因此不能改变lambda表达式中自由变量的值,若是改变,那么首先就没法编译经过。
对于引用类型(如ArrayList),final指的是引用指向的类始终不变,进行add操做是容许的,可是应该保证变量的线程安全。
代码以下所示:
public class Outer {
    public AnnoInner getAnnoInner(int x) {
        int y = 100;
        return new AnnoInner() {
            int z = 100;
 
            @Override
            public int add() {
                return x + y + z;
            }
        };
    }
 
    public AnnoInner AnnoInnergetAnnoInner1(List<Integer> list1) {
        List<Integer> list2 = new ArrayList<>(Arrays.asList(1, 2, 3));
        return ()->{
            list2.add(123);
            int count = 0;
            Iterator<Integer> it = list1.iterator();
            while (it.hasNext()){
                count+=it.next();
            }
            Iterator<Integer> it1 = list2.iterator();
            while (it1.hasNext()){
                count+=it1.next();
            }
            return count;
        };
    }
 
    @Test
    public void test(){
        AnnoInner res = new Outer().AnnoInnergetAnnoInner1(new ArrayList<>(Arrays.asList(1,2,3)));
        System.out.println(res.add());
    }
 
}
 
interface AnnoInner {
    int add();
}
最后返回135
5.接口的静态方法和默认方法
java8对于接口作出了种种改进,使得咱们能够在接口中实现默认方法和静态方法,见Comparator接口完整定义
@FunctionalInterface
public interface Comparator<T> {
 
    int compare(T o1, T o2);
 
    boolean equals(Object obj);
 
    default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }
 
    default Comparator<T> thenComparing(Comparator<? super T> other) {
        Objects.requireNonNull(other);
        return (Comparator<T> & Serializable) (c1, c2) -> {
            int res = compare(c1, c2);
            return (res != 0) ? res : other.compare(c1, c2);
        };
    }
 
    default <U> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        return thenComparing(comparing(keyExtractor, keyComparator));
    }
 
    default <U extends Comparable<? super U>> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        return thenComparing(comparing(keyExtractor));
    }
 
    default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
        return thenComparing(comparingInt(keyExtractor));
    }
 
    default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
        return thenComparing(comparingLong(keyExtractor));
    }
 
    default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        return thenComparing(comparingDouble(keyExtractor));
    }
 
    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
        return Collections.reverseOrder();
    }
 
    @SuppressWarnings("unchecked")
    public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
        return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
    }
 
    public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(true, comparator);
    }
 
    public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(false, comparator);
    }
 
    public static <T, U> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        Objects.requireNonNull(keyExtractor);
        Objects.requireNonNull(keyComparator);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
                                              keyExtractor.apply(c2));
    }
 
    public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }
 
    public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
    }
 
    public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
    }
 
    public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
    }
}
在比较器接口中定义了若干用于比较和键提取的静态方法和默认方法,默认方法的使用使得方法引用更加方便,例如使用java.util.Objects类中的静态方法isNull和nonNull能够在Stream中很方便的进行null的断定(以后会有对于stream的介绍)。可是在接口中引入默认方法设计到一个问题,即
(1).接口中的默认方法和父类中方法的冲突问题
(2).接口之间引用的冲突问题
对于第一个冲突,java8规定类中的方法优先级要高于接口中的默认方法,因此接口中默认方法复写Object类中的方法是没有意义的,由于全部的接口都默认继承自Object类使得默认方法必定会被覆盖。
对于第二个冲突,java8强制要求子类必须复写接口中冲突的方法。以下所示:
public class LambdaTest5 implements myInterface1, myInterface2 {
    @Override
    public void getName() {
        myInterface1.super.getName();
    }
 
    public static void main(String[] args) {
        new LambdaTest5().getName();
    }
}
 
interface myInterface1 {
    default void getName() {
        System.out.println("myInterface1 getName");
    }
 
    ;
}
 
interface myInterface2 {
    default void getName() {
        System.out.println("myInterface2 getName");
    }
}
强制使用myInterface1中的getName方法
相关文章
相关标签/搜索