java8新特性之lamda表達式

lamda表達式

java8中lamda表達式的引入,標誌着java開始擁抱主流函數式編程語言,其實lamda表達式並非什麼新鮮事物,在JavaScript中早已存在。做爲經典語言引入lamda表達式以及java8中的其餘新特性,如流式處理,會必定程度上讓代碼更簡潔,固然仍是沒法和JavaScript這類語言相比,但這也正是各自所處的角色的不一樣決定的。java

lamda表達式的格式

  1. 參數類型聲明:能夠不須要聲明參數類型,編譯器會識別參數值。
  2. 參數圓括號(可選):在單個參數時能夠不使用括號,多個參數時必須使用。
  3. 大括號和return關鍵字(可選):若是隻有一個表達式,則能夠省略大括號和return關鍵字,編譯器會自動的返回值;相對的,在使用大括號的狀況下,則必須指明返回值。

lamda表達式例子

以Collections.sort爲例編程

/**
 * @author xiantao.wu
 * @create 2018/8/1514:09
 **/
public class TestLamda {
    public static void main(String[] args) {
        //原生方法
        List<Student> students1=getStudentList();
        Collections.sort(students1, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge().compareTo(o2.getAge());
            }
        });
        students1.forEach(in-> System.out.print(in.getName()+in.getAge()+"|"));
        System.out.println("原生方法");


        //第一種狀況:傳入靜態方法
        List<Student> students2=getStudentList();
        Collections.sort(students2, (s1, s2) -> Student.sortByAgeStatic(s1, s2));
        students2.forEach(in-> System.out.print(in.getName()+in.getAge()+"|"));
        System.out.println("第一種狀況:傳入靜態方法");


        //第二種狀況:傳入實例方法
        List<Student> students3=getStudentList();
        Collections.sort(students3, (s1, s2) -> new Student().sortByAgeNonStatic(s1, s2));
        students3.forEach(in-> System.out.print(in.getName()+in.getAge()+"|"));
        System.out.println("第二種狀況:傳入實例方法");


        //第三種請款,不適用大括號
        List<Student> students4=getStudentList();
        Collections.sort(students4, (s1, s2) -> s1.getAge().compareTo(s2.getAge()));
        students4.forEach(in-> System.out.print(in.getName()+in.getAge()+"|"));
        System.out.println("第三種請款,不適用大括號");


        //第四種狀況,使用大括號
        List<Student> students5=getStudentList();
        Collections.sort(students5, (s1, s2) -> {
            return s1.getAge().compareTo(s2.getAge());
        });
        students5.forEach(in-> System.out.print(in.getName()+in.getAge()+"|"));
        System.out.println("第四種狀況,使用大括號");

    }

    private static List<Student> getStudentList() {
        List<Student> studentList = new ArrayList();
        for (int i = 0; i < 5; i++) {
            Student stu = new Student();
            stu.setAge(new Random().nextInt(10));
            stu.setName("tom");
            studentList.add(stu);
        }
        return studentList;
    }


}


/**
 * @author xiantao.wu
 * @create 2018/8/1515:44
 **/
public class Student {
    private Integer age;
    private String name;


    public static int sortByAgeStatic(Student o1, Student o2) {
        return o1.getAge().compareTo(o2.getAge());
    }


    public int sortByAgeNonStatic(Student o1, Student o2) {
        return o1.getAge().compareTo(o2.getAge());
    }


    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
複製代碼

測試結果:

tom1|tom2|tom3|tom3|tom9|原生方法
tom0|tom0|tom1|tom2|tom4|第一種狀況:傳入靜態方法
tom2|tom2|tom3|tom4|tom8|第二種狀況:傳入實例方法
tom1|tom1|tom1|tom4|tom8|第三種請款,不適用大括號
tom0|tom1|tom1|tom3|tom7|第四種狀況,使用大括號複製代碼
相關文章
相關標籤/搜索