一. 总概述:
二. 概述
面向对象中的缺陷:需要创建对象,然后使用对象,使得语句有点冗余,在jdk1.8的时候,加入了lambda表达式,即函数式编程思想。使得编程者只“关注做什么事”,而省略了一些步骤
三. 格式:(参数名称)-> {方法体}
四. 具体案列
1. 无参测试:
public class Main {
public static void main(String[] args) {
//面向对象式的创建线程!
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
//匿名内部类创建一个线程
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"匿名内部类创建的线程!");
}
}).start();
//lambda标准式线程
new Thread(()-> System.out.println(Thread.currentThread().getName()+"lambda标准式创建的线程")).start();
//lambda简洁式线程
// 1. 由于无参数,用括号代替
// 2. 由于方法体只有一句话,省略大括号和分号
new Thread(()-> System.out.println(Thread.currentThread().getName()+"lambda简洁式创建的线程")).start();
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"对象创建的线程!");
}
}
2. 只有一个参数
public class MainTest {
public static void main(String[] args) {
String name = "旺财";
//普通创建对象调用方法
new FunctionTest(name).getFood(new Function() {
@Override
public void getFood(String name) {
System.out.println(name + "得到了食物");
}
});
//标准lumbda标准调用方法
new FunctionTest(name).getFood((String rs)-> System.out.println(rs+"得到了食物"));
//简洁式lumbda标准调用方法
// 1. 由于只有参数,省略括号、参数数据类型。
// 2. 由于方法体只有一句话,省略大括号和分号
new FunctionTest(name).getFood(rs-> System.out.println(rs+"123"));
}
}
class FunctionTest {
private String name;
public FunctionTest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void getFood(Function function){
function.getFood(name);
}
}
interface Function<T> {
void getFood(String name);
多个参数
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
//按年龄降序排列
Student stu = new Student("tom",13);
Student stu1 = new Student("james",15);
Student stu2 = new Student("Linda",14);
Student stu3 = new Student("jack",12);
ArrayList<Student> students = new ArrayList<>();
Collections.addAll(students,stu,stu1,stu2,stu3);
//普通方法按年龄排序
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAge() - o1.getAge();
}
});
for (Student student : students) {
System.out.println(student.getName()+"->"+student.getAge());
}
System.out.println("========================");
//标准式lumbda排序
Collections.sort(students,(Student o1, Student o2)-> {
return o2.getAge()-o1.getAge();
});
for (Student student : students) {
System.out.println(student.getName()+"->"+student.getAge());
}
System.out.println("========================");
//简介式lumbda
//1. 含有两个参数,可以省略数据类型
//2. 方法体只有一句,由于需要返回值类型,可以省略return ,大括号和分号
Collections.sort(students,(o1, o2) -> o1.getAge()-o2.getAge());
//集合转化为流输出
students.stream().forEach(rs-> System.out.println(rs.getName()+"->"+rs.getAge()));
}
}
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = 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;
}
}