基本语法:
(parameters) -> expression
或者:(parameters) ->{ statements; }
案例:`
public static void main(String[] args) {
//新线程
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello world !");
}
}).start();
//??做什么
new Thread(() -> System.out.println("Hello world !")).start();
}
可以用lambda替换原来的只有一个方法的接口
数组排序
public static void main(String[] args) {
String[] players = {"zhansgan", "lisi", "wangwu", "zhaoliu", "wangmazi"};
// Arrays.sort(players);
// java8之前:
// Arrays.sort(players, new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
//Lambda
Arrays.sort(players, (o1, o2) -> o1.compareTo(o2));
for(int i = 0; i < players.length; i++) {
System.out.println(players[i]);
}
}
方法引用
静态方法引用 类名 :: 静态方法名
实例方法引用 对象名(引用名) :: 实例方法名
类方法引用 类名 :: 实例方法名
构造方法引用 类名 :: new
实体类:
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 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 static int compareByWeight(Apple a1, Apple a2) {
double diff = a1.getWeight() - a2.getWeight();
return new Double(diff).intValue();
}
}
静态方法引用
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> appleList = Arrays.asList(apple1, apple2, apple3, apple4);
//lambda 表达式形式
appleList.sort((Apple a1, Apple a2) -> {
return new Double(a1.getWeight() - a2.getWeight()).intValue();
});
//静态方法引用形式(可以看出引用方法比上面的更加简单
// appleList.sort(Apple::compareByWeight);
appleList.forEach(apple -> System.out.println(apple));
}
(2)实例方法引用
AppleComparator
public class AppleComparator {
public int compareByWeight(Apple a1, Apple a2) {
double diff = a1.getWeight() - a2.getWeight();
return new Double(diff).intValue();
}
}
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> appleList = Arrays.asList(apple1, apple2, apple3, apple4);
//lambda 表达式形式
//appleList.sort((Apple a1, Apple a2) -> {
// return new Double(a1.getWeight() - a2.getWeight()).intValue();
//});
//实例方法引用
AppleComparator comparator = new AppleComparator();
appleList.sort(comparator::compareByWeight);
appleList.forEach(apple -> System.out.println(apple));
}
(3)类方法引用
实体类:
package com.zpark.demo;
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 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 int compareByWeight(Apple other) {
double diff = this.getWeight() - other.getWeight();
return new Double(diff).intValue();
}
}
引用:
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> appleList = Arrays.asList(apple1, apple2, apple3, apple4);
//lambda 表达式形式
// appleList.sort((Apple a1, Apple a2) -> {
// return new Double(a1.getWeight() - a2.getWeight()).intValue();
// });
appleList.sort(Apple::compareByWeight);
appleList.forEach(apple -> System.out.println(apple));
}
(4)构造方法引用
int age;
public ConstructionMethodTest() {
System.out.println("...." + age);
}
public static void main(String[] args) {
Supplier<ConstructionMethodTest> aNew = ConstructionMethodTest::new;
aNew.get();
aNew.get();
}