JDK8新特性-Lambda
1.认识Lambda
2.Lambda表达式的省略写法
进一步简化Lambda表达式的写法
package cn.hdc.oop5.lambda;
import java.util.Arrays;
import java.util.Comparator;
public class Test2 {
public static void main(String[] args) {
double[] price = {99.8, 128, 100};
// Arrays.setAll(price, (int value) -> {
// return price[value] * 0.8;
// });
/**
* 参数类型可以省略不写
* 如果只有一个参数,参数类型可以省略同时()也可以省略
*/
// Arrays.setAll(price, value -> {
// return price[value] * 0.8;
// });
/**
* 如果Lambda表达式中的方法体代码只有一行代码,可以省略大括号不写,同时要省略分号!此时,如
* 果这行代码是return语句,也必须去掉return不写,
*/
Arrays.setAll(price, value -> price[value] * 0.8);
System.out.println("-------------------------------------------------------");
Student[] students = new Student[4];
students[0] = new Student("蜘蛛精", 169.5, 23);
students[1] = new Student("紫霞", 163.8, 26);
students[2] = new Student("紫霞", 163.8, 26);
students[3] = new Student("至尊宝", 167.5, 24);
// Arrays.sort(students, new Comparator<Student>() {
// @Override
// public int compare(Student o1, Student o2) {
// return (int) (o1.getHeight() - o2.getHeight());
// }
// });
// Arrays.sort(students, (Student o1, Student o2) -> {
// return Double.compare(o1.getHeight(), o2.getHeight());
// });
// Arrays.sort(students, (o1, o2) -> {
// return Double.compare(o1.getHeight(), o2.getHeight());
// });
Arrays.sort(students, (o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()));
}
}
class Student {
private String name;
private double height;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Student(String name, double height, int age) {
this.name = name;
this.height = height;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", height=" + height +
", age=" + age +
'}';
}
}
3.方法引用
Lambda表达式用来简化函数式接口的匿名内部类
方法引用进一步简化Lambda表达式
方法引用的标志性符号:“ :: ”
1.静态方法的引用
package cn.hdc.oop5.staticMethod;
import cn.hdc.oop5.lambda.Student;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("蜘蛛精", 169.5, 23);
students[1] = new Student("紫霞", 163.8, 26);
students[2] = new Student("紫霞", 163.8, 26);
students[3] = new Student("至尊宝", 167.5, 24);
Arrays.sort(students, (o1, o2) -> o1.getAge() - o2.getAge());
Arrays.sort(students, (o1, o2) -> CompareByData.compareByAge(o1, o2));
/**
* 如果某个Lambda表达式里只是调用一个静态方法,并且前后参数的形式一致,就可以使用静态方法引用。
*/
Arrays.sort(students, CompareByData::compareByAge);
}
}
package cn.hdc.oop5.staticMethod;
import cn.hdc.oop5.lambda.Student;
public class CompareByData {
public static int compareByAge(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
}
2.实例方法的引用
package cn.hdc.oop5.staticMethod;
import cn.hdc.oop5.lambda.Student;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("蜘蛛精", 169.5, 23);
students[1] = new Student("紫霞", 163.8, 26);
students[2] = new Student("紫霞", 163.8, 26);
students[3] = new Student("至尊宝", 167.5, 24);
Arrays.sort(students, (o1, o2) -> new CompareByData().compareByHeight(o1, o2));
/**
* 如果某个Lambda表达式里只是调用一个实例方法,并且前后参数的形式一致,就可以使用实例方法引用
*/
Arrays.sort(students, new CompareByData()::compareByHeight);
}
}
package cn.hdc.oop5.staticMethod;
import cn.hdc.oop5.lambda.Student;
public class CompareByData {
public int compareByHeight(Student s1, Student s2) {
return (int) (s1.getHeight() - s2.getHeight());
}
}
3.特定类型方法的引用
package cn.hdc.oop5.special;
import java.util.Arrays;
import java.util.Comparator;
public class specialImport {
public static void main(String[] args) {
String[] names = {"boby", "angela", "Andy", "dlei", "caocao", "Babo", "jack", "Cici"};
// Arrays.sort(names);
// Arrays.sort(names, new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareToIgnoreCase(o2);
// }
// });
// Arrays.sort(names, (o1, o2) -> o1.compareToIgnoreCase(o2));
/**
* 如果某个Lambda表达式里只是调用一个实例方法,
* 并且前面参数列表中的第一个参数是作为方法的主调
* 后面的所有参数都是作为该实例方法的入参的,则此时就可以使用特定类型的方法引用。
*/
Arrays.sort(names, String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
}
}
4.构造器的引用
package cn.hdc.oop5.constructor;
public class constructorImport {
public static void main(String[] args) {
// CreateCar cc = new CreateCar() {
// @Override
// public Car create(String name, double price) {
// return new Car(name, price);
// }
// };
// CreateCar cc = (name, price) -> new Car(name, price);
/**
* 如果某个Lambda表达式里只是在创建对象,并且前后参数情况一致,就可以使用构造器引用
*/
CreateCar cc = Car::new;//构造器引用
Car C = cc.create("BMW", 49.9);
System.out.println(C);
}
}
interface CreateCar {
Car create(String name, double price);
}
package cn.hdc.oop5.constructor;
public class Car {
private String name;
private double price;
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
public Car() {
}
public Car(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}