1、基本语法:
(parameters) -> expression
或者:(parameters) ->{ statements; }
例如:
package zpark;
public class ThreadTest {
public static void main(String[] args) {
//新线程(之前方法)
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello you!");
}
}).start();
//lambda表达式 与上面打印结果相同
//可以用lambda表达式替换的只有一个方法的接口
new Thread(() -> System.out.println("Hello you!") ).start();
/**
* Hello you!
* Hello you!
*/
}
}
2、排序:
package zpark;
import java.util.Arrays;
import java.util.Comparator;
public class ThreadTest2 {
public static void main(String[] args) {
String[] players = {"haha1","bebe22","zdzd333","qiqu113","llll999"};//1
//排序
//java8之前
Arrays.sort(players, new Comparator<String>() {//2
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
Arrays.sort(players);
for (int i=0 ; i<players.length ; i++)
System.out.println("排序: " + players[i]); //3
/**
* 排序: bebe
* 排序: haha
* 排序: llll
* 排序: qiqu
* 排序: zdzd
*/
Arrays.sort(players,(o1,o2) -> o1.compareTo(o2)); //4 //
// (1)先运行123 使用匿名内部类根据 surname 排序 players
// (2)在运行134 使用 lambda 排序,根据 surname
//使用匿名内部类根据 name lenght 排序 players
Arrays.sort(players, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o1.length() - o2.length());
}
});
//使用Lambda,根据name length
Arrays.sort(players,(o1, o2) -> (o1.length() - o2.length()) );
for (int i=0 ; i<players.length ; i++)
System.out.println("排序: " + players[i]);
/**
* 排序: haha1
* 排序: bebe22
* 排序: zdzd333
* 排序: qiqu113
* 排序: llll999
*/
//使用匿名内部类排序 players, 根据最后一个字母
Arrays.sort(players, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o1.charAt(o1.length() -1 ) - o2.charAt(o2.length() -1 ));
}
});
//使用Lambda,根据最后一个字母
Arrays.sort(players,(o1,o2) ->(o1.charAt(o1.length()-1) - o2.charAt(o2.length()-1)));
for (int i=0 ; i<players.length ; i++)
System.out.println("排序: " + players[i]);
/**
* 排序: haha1
* 排序: bebe22
* 排序: qiqu113
* 排序: zdzd333
* 排序: llll999
*/
}
}
3,、方法引用
函数式接口的实例可以通过 lambda 表达式、 方法引用、构造方法引用来创建。方法引用是 lambda 表达式的语法糖,任何用方法引用的地方都可由lambda表达式替换,但是并不是所有的lambda表达式都可以用方法引用来替换。
例如:简单的打印集合中所有元素
package zpark;
import java.util.Arrays;
import java.util.List;
public class ThraedTest4 {
public static void main(String[] args) {
List<String> list = Arrays.asList("xuxiaoxiao", "xudada", "xuzhongzhong");
//方法引用 打印集合所有元素
list.forEach(value -> System.out.println(value));
/**
* xuxiaoxiao
* xudada
* xuzhongzhong
*/
//使用方法引用的方式,和上面的输出是一样的,方法引用使用的是双冒号(::)
list.forEach(System.out::println);
/**
* xuxiaoxiao
* xudada
* xuzhongzhong
*/
}
}
分类:使用::
类别 | 使用形式 |
静态方法引用 | 类名::静态方法名 |
实例方法引用 | 对象名(引用名)::实例方法名 |
类方法引用 | 类名::实例方法名 |
构造方法应用 | 类名::new |
(1)静态方法引用
有一个苹果的List,现在需要根据苹果的重量进行排序。List 的 sort 函数接收一个 Comparator 类型的参数,Comparator 是一个函数式接口,接收两个参数,返回一个int值1
Apple的静态方法compareByWeight正好符合Comparator函数式接口,所以可以使用:
Apple::compareByWeight 静态方法引用来替代lambda表达式
Apple实体类:
package zpark;
import scala.collection.generic.BitOperations;
import java.util.Arrays;
import java.util.Comparator;
//ThreadTest3 静态方法引用
//定义实体类
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 "ThreadTest3{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", weight=" + weight +
'}';
}
public static int comW(Apple a1,Apple a2){
double diff = a1.getWeight() - a2.getWeight();
return new Double(diff).intValue();
}
}
Apple测试类:
package zpark;
import java.util.Arrays;
import java.util.List;
public class AppleTest {
public static void main(String[] args) {
Apple apple1 = new Apple("红富士","red",50d);
Apple apple2 = new Apple("红富","yello",502d);
Apple apple3 = new Apple("富士","green",500d);
Apple apple4 = new Apple("红士","cyan",55d);
Apple apple5 = new Apple("红","black",100d);
//根据苹果的重量进行排序
List<Apple> appleList = Arrays.asList(apple1,apple2,apple3,apple4,apple5);
// System.out.println(appleList);
appleList.sort(Apple::comW);
appleList.forEach(apple -> System.out.println(apple));
/**
* ThreadTest3{name='红富士', color='red', weight=50.0}
* ThreadTest3{name='红士', color='cyan', weight=55.0}
* ThreadTest3{name='红', color='black', weight=100.0}
* ThreadTest3{name='富士', color='green', weight=500.0}
* ThreadTest3{name='红富', color='yello', weight=502.0}
*/
}
}
(2)类方法引用
Apple2实体类:
package zpark;
//定义实体类
public class Apple2 {
private String category;
private String color;
private double weight;
public Apple2(String category, String color, double weight) {
this.category = category;
this.color = color;
this.weight = weight;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
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 "Apple2{" +
"category='" + category + '\'' +
", color='" + color + '\'' +
", weight=" + weight +
'}';
}
public int compareW(Apple2 other){
double diff = this.getWeight() - other.getWeight();
return new Double(diff).intValue();
}
}
Apple2测试类:
package zpark;
import java.util.Arrays;
import java.util.List;
public class Apple2Test {
public static void main(String[] args) {
Apple2 apple1 = new Apple2("红富士","red",50d);
Apple2 apple2 = new Apple2("红富","yello",502d);
Apple2 apple3 = new Apple2("富士","green",500d);
Apple2 apple4 = new Apple2("红士","cyan",55d);
Apple2 apple5 = new Apple2("红","black",100d);
List<Apple2> apple2List = Arrays.asList(apple1,apple2,apple3,apple4,apple5);
//lambda 表达式形式
apple2List.sort((Apple2 a1,Apple2 a2) -> {
return new Double(apple1.getWeight() - apple2.getWeight()).intValue();
});
//这里是类方法引用
apple2List.sort(Apple2::compareW);
apple2List.forEach(apple21 -> System.out.println(apple21));
/**
* Apple2{category='红富士', color='red', weight=50.0}
* Apple2{category='红士', color='cyan', weight=55.0}
* Apple2{category='红', color='black', weight=100.0}
* Apple2{category='富士', color='green', weight=500.0}
* Apple2{category='红富', color='yello', weight=502.0}
*/
}
}
(3)实例方法引用
package zpark;
public class Apple3 {
private String name;
private String color;
private Double weight;
public Apple3(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 "Apple3{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", weight=" + weight +
'}';
}
// 实例方法
public static class AppleComparator{
public int compareByWeight(Apple a1,Apple a2){
double deff = a1.getWeight() - a2.getWeight();
int i = new Double(deff).intValue();
return i;
}
}
}
Apple3测试类
package zpark;
import java.util.Arrays;
import java.util.List;
public class Apple3Test {
/**
* 实例方法引用
*/
public static void main(String[] args) {
Apple3 apple1 = new Apple3("红富士", "Red", 280d);
Apple3 apple2 = new Apple3("蛇果", "Yello", 100d);
Apple3 apple3 = new Apple3("blue", "Red", 320d);
Apple3 apple4 = new Apple3("pink", "Green", 300d);
List<Apple3> appleList = Arrays.asList(apple1, apple2, apple3, apple4);
// lambda表达式
// apples.sort((Apple a1,Apple a2) -> {
// return new Double(a1.getWeight()- a2.getWeight()).intValue();
// });
// 实例方法引用形式
Apple3.AppleComparator appleComparator = new Apple3.AppleComparator();
appleList.sort(appleComparator::compareByWeight
);
// 遍历输出
appleList.forEach(apple -> System.out.println(apple));
/**
* Apple3{name='蛇果', color='Yello', weight=100.0}
* Apple3{name='红富士', color='Red', weight=280.0}
* Apple3{name='pink', color='Green', weight=300.0}
* Apple3{name='blue', color='Red', weight=320.0}
*/
}
}
(4)构造方法引用
package zpark;
import java.util.function.Supplier;
public class ThreadTest5 {
int age;
public ThreadTest5(){
System.out.println("......输出...."+age);
}
public static void main(String[] args) {
Supplier<ThreadTest5> aNew = ThreadTest5::new;
aNew.get();
/**
* ......输出....0
*/
}
}