Lambda是一种匿名函数,,简单的说,它是没有声明的方法,也即没有访问修饰付,返回值声明和名字,java8的新特性。
当接口中只有一个抽象式方法这样的类叫做函数式接口,可以通过Lambda表达式创建接口。
表达式的语法:
基本语法:(parameters)->exparession
或者:(parameters)-> {statements}
传统和Lambda表达式的对比:
public class Lambda {
public static void main(String[] args) {
//Runable是一个匿名内部类
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
}).start();
//Lambda表达式
new Thread(() -> System.out.println("hello")).start();
}
}
两种方式的效果是一样的,也就是说函数式接口可以用Lambda表达式代替
实例:
实体类
public class Apple {
private String name;
private String color;
private double weight;
//构造方法
public Apple(String name, String color, double weight) {
this.name = name;
this.color = color;
this.weight = weight;
}
public static int compareByWeight(Apple a1,Apple a2){
double diff = a1.getWeight()-a2.getWeight();
return new Double(diff).intValue();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", weight=" + weight +
'}';
}
}
静态方法引用(public所有的静态方法都可以用)
import java.util.Arrays;
import java.util.List;
public class Apple1 {
//静态方法引用
public static void main(String[] args) {
Apple s1 = new Apple("红富士","Red",280);
Apple s2 = new Apple("花牛","Red",380);
Apple s3 = new Apple("黄果树","Yellow",230);
List<Apple> apple = Arrays.asList(s1,s2,s3);
//lambdab表达式形式
apple.sort((Apple a1,Apple a2) ->{
return new Double(s1.getWeight() - s2.getWeight()).intValue();
});
apple.forEach(value -> System.out.println(apple));
}
}
实体类方法引用
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Apple2 {
//实例方法引用
public static void main(String[] args) {
Apple apple1 = new Apple("红富士", "Red", 280);
Apple apple2 = new Apple("冯心", "Yello", 470);
Apple apple3 = new Apple("哈哈", "Red", 320);
Apple apple4 = new Apple("小小", "Green", 300);
List<Apple> apple = Arrays.asList(apple1,apple2,apple3,apple4);
//Lambda
apple.sort((Apple a1,Apple a2) ->{
return new Double(a1.getWeight() - a2.getWeight()).intValue();
});
Apple2 a = new Apple2();
apple.sort(a::compareWeight);
apple.forEach(value -> System.out.println(apple));
}
public int compareWeight(Apple s1,Apple s2){
double diff = s1.getWeight() - s2.getWeight();
return new Double(diff).intValue();
}
}
类方法引用
mport java.util.Arrays;
import java.util.List;
public class Apple3 {
//类方法引用
public static void main(String[] args) {
Apple apple1 = new Apple("红富士", "Red", 280);
Apple apple2 = new Apple("冯心", "Yello", 470);
Apple apple3 = new Apple("哈哈", "Red", 320);
Apple apple4 = new Apple("小小", "Green", 300);
List<Apple> apple = Arrays.asList(apple1, apple2, apple3, apple4);
//Lambda
apple.sort((Apple a1,Apple a2) ->{
return new Double(a1.getWeight() - a2.getWeight()).intValue();
});
apple.sort(Apple::compareByWeight);
apple.forEach(value -> System.out.println(apple));
}
构造方法
import java.util.function.Supplier;
public class Apple4 {
int age;
public static void main(String[] args) {
//对象提取器
Supplier<Apple4> aNew = Apple4::new;
aNew.get();
}
public Apple4(){
System.out.println("..." + age);
}
}