引入
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/70be52a122414082b199d7dff2dfdbde.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b5a53869530245f0ba7c1b8e93c59f39.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b87a241555e141d1b63cc3f68826c132.png)
一、Lambda表达式
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/14d540149cae409f815f6e5dc9c428d2.png)
1.1 快速入门
package com.gyh;
import java.util.Comparator;
public class LambdaTest {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("hsp");
}
};
runnable.run();
Runnable runnable2 = () -> System.out.println("hsp");
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
int compare = comparator.compare(1, 2);
System.out.println(compare);
Comparator<Integer> comparator2 = (o1, o2) -> Integer.compare(o1,o2);
Comparator<Integer> comparator3 = Integer::compareTo;
}
}
1.2 Lambda语法
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ead0fc7e1f2641868f500e6d15337c47.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2dac84451eb8487688271144df570d54.png)
package com.gyh;
import java.util.Comparator;
import java.util.function.Consumer;
public class LambadaTest1 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("hsp");
}
};
Runnable runnable2 = () -> {
System.out.println("hsp");
};
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Consumer<String> consumer2 = (String s) -> {
System.out.println(s);
};
Consumer<String> consumer3 = (s) -> {
System.out.println(s);
};
Consumer<String> consumer4 = s -> {
System.out.println(s);
};
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
Comparator<Integer> comparator1 = (x, y) -> {
System.out.println("实现函数式接口方法!");
return Integer.compare(x, y);
};
Comparator<Integer> comparator2 = (x,y) -> Integer.compare(x, y);
}
}
二、函数式(Functional)接口
2.1 函数式(Functional)接口介绍
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/d585d0c2e33445f9a5817145301532d3.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/8feced276c864dc5937afea13b212226.png)
2.2 Java内置函数式接口
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/c18d9942d543496fa746a8bed3286814.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/aa911418e1984e4991813b45cbed03f6.png)
package com.gyh;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class LambdaTest2 {
public static void main(String[] args) {
test();
}
public static void test() {
List<String> strings = Arrays.asList("北京", "东京", "西京", "南京", "天津");
List<String> str = filterList(strings, s -> s.contains("京"));
System.out.println(str);
}
public static void happyTime(double money, Consumer<Double> con) {
con.accept(money);
}
private static List<String> filterList(List<String> str, Predicate<String> p) {
ArrayList<String> newsStr = new ArrayList<>();
for (String s : str) {
if (p.test(s)) {
newsStr.add(s);
}
}
return newsStr;
}
}
三、方法引用与构造器引用
3.1 方法引用
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/bd8aca9168574d37a058fe6fb1d000c4.png)
package com.gyh;
import java.util.Comparator;
import java.util.function.Consumer;
public class LambdaTest3 {
public static void main(String[] args) {
}
public static void test1() {
Consumer<String> con1 = str -> System.out.println(str);
con1.accept("北京");
Consumer<String> con2 = System.out::println;
}
public static void test2() {
Comparator<Integer> com1 = (i1,i2) -> Integer.compare(i1,i2);
Comparator<Integer> com2 = Integer::compareTo;
}
public static void test3() {
Comparator<String> com1 = (i1,i2) -> i1.compareTo(i2);
Comparator<String> com2 = String::compareTo;
}
}
3.2 构造器引用
package com.gyh;
import java.util.function.Function;
import java.util.function.Supplier;
public class LambdaTest4 {
public void test1() {
Supplier<String> stringSupplier1 = new Supplier<String>() {
@Override
public String get() {
return new String();
}
};
Supplier<String> stringSupplier2 = () -> new String();
Supplier<String> stringSupplier3 = String::new;
}
public void test2() {
Function<String, String> f1 = (name) -> new String(name);
Function<String, String> f2 = String::new;
}
public void test3() {
Function<Integer, String[]> f1 = (length) -> new String[length];
Function<Integer, String[]> f2 = String[]::new;
}
}
四、强大的Stream API
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/aa0e292bd61a49a2853f7511ad8629c3.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/082b50b8df824a0f9f1342ea6c15538c.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/576b168540014528a84bc6a5cb021cd5.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/af1710ead4ea4c1c8fdce08cc43802f8.png)
4.1 创建 Stream 的4种方式
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/943c7d40048c46b989238fd407013da0.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e341a91ac2b949ac8e4499afabdcf098.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/43480a40beed46aea62679054e0e63a6.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/977df060fb8e4772b0f358a438aa53ea.png)
package com.gyh;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamAPITest {
public static void main(String[] args) {
List<Employee> employees = EmployeeData.getEmployees();
Stream<Employee> stream = employees.stream();
Stream<Employee> employeeStream = employees.parallelStream();
int[] arr = {1, 2, 3, 4, 5, 6};
IntStream stream1 = Arrays.stream(arr);
Employee[] objects = (Employee[]) employees.toArray();
Stream<Employee> stream2 = Arrays.stream(objects);
Stream<Employee> objects1 = Stream.of(objects);
Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
Stream.generate(Math::random).limit(10).forEach(System.out::println);
}
}
class EmployeeData {
public static List<Employee> getEmployees() {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("马化腾", 34, 1001, 6000.38));
employees.add(new Employee("马云", 12, 1002, 9876.12));
employees.add(new Employee("刘强东", 33, 1003, 3000.82));
employees.add(new Employee("雷军", 26, 1004, 7657.37));
employees.add(new Employee("李彦宏", 65, 1005, 5555.32));
employees.add(new Employee("比尔盖茨", 42, 1006, 9500.43));
employees.add(new Employee("任正非", 26, 1007, 4333.32));
employees.add(new Employee("扎克伯格", 35, 1008, 2500.32));
return employees;
}
}
class Employee {
private String name;
private int age;
private int id;
private double salary;
public Employee(String name, int age, int id, double salary) {
this.name = name;
this.age = age;
this.id = id;
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return age == employee.age &&
id == employee.id &&
Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, id, salary);
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", salary=" + salary +
'}';
}
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
4.2 Stream 的中间操作
4.2.1 筛选与切片
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/38d5762409bf4218a7a0158a7f121c62.png)
package com.gyh;
import java.util.List;
public class StreamAPITest1 {
public static void main(String[] args) {
List<Employee> employees = EmployeeData.getEmployees();
employees.stream()
.filter(e -> e.getSalary() > 7000)
.forEach(System.out::println);
employees.stream()
.limit(3)
.forEach(System.out::println);
employees.stream()
.skip(3)
.forEach(System.out::println);
employees.stream()
.distinct()
.forEach(System.out::println);
}
}
4.2.2 映射
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/e407c78da76c4459a8664b4cc8bf5787.png)
package com.gyh;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class StreamAPITest2 {
public static void main(String[] args) {
List<String> strings = Arrays.asList("aa", "bb", "cc", "dd");
strings.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
strings.stream()
.map(StreamAPITest2::fromStringToStream)
.forEach(s -> {
s.forEach(System.out::println);
});
strings.stream()
.flatMap(StreamAPITest2::fromStringToStream)
.forEach(System.out::println);
}
public static Stream<Character> fromStringToStream(String str) {
ArrayList<Character> characters = new ArrayList<>();
for (Character s : str.toCharArray()) {
characters.add(s);
}
return characters.stream();
}
}
4.2.3 排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/012295ae29ff4cfb9988d72cef097870.png)
package com.gyh;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class StreamAPITest3 {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(12, 43, 65, 34, 87, 0, -98, 7);
integers.stream().sorted().forEach(System.out::println);
EmployeeData.getEmployees()
.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary))
.forEach(System.out::println);
}
}
4.3 Stream 的终止操作
4.3.1 匹配与查找
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/67d7058199194956b57c4e23eefe841c.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/8307ca9f1e034d3290695712e4c37884.png)
package com.gyh;
import java.util.List;
import java.util.Optional;
public class StreamAPITest4 {
public static void main(String[] args) {
List<Employee> employees = EmployeeData.getEmployees();
boolean b = employees.stream()
.allMatch(e -> e.getAge() > 18);
boolean b1 = employees.stream()
.anyMatch(e -> e.getSalary() > 10000);
boolean b2 = employees.stream()
.noneMatch(e -> e.getName().contains("雷"));
Optional<Employee> first = employees.stream()
.findFirst();
Optional<Employee> any = employees.stream()
.findAny();
long count = employees.stream().filter(e -> e.getAge() > 12).count();
Optional<Employee> max = employees.stream()
.max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
Optional<Employee> min = employees.stream()
.min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
employees.stream().forEach(System.out::println);
employees.forEach(System.out::println);
}
}
4.3.2 归约
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/1e09cefa50d04366b42a9365bfabd0d8.png)
package com.gyh;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class StringAPITest5 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer reduce = list.stream().reduce(0, Integer::sum);
List<Employee> employees = EmployeeData.getEmployees();
Optional<Double> reduce1 = employees.stream()
.map(Employee::getSalary)
.reduce(Double::sum);
}
}
4.3.3 收集
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/9b4bb3c358d843cc8229982d98105937.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/199ae41776d84d93ad4a8d24cb91c0a9.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/d082a8f2247f4213985ebd3c0be1644c.png)
package com.gyh;
import java.util.List;
import java.util.stream.Collectors;
public class StreamAPITest6 {
public static void main(String[] args) {
List<Employee> employees = EmployeeData.getEmployees();
List<Employee> collect = employees.stream()
.filter(e -> e.getSalary() > 6000)
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
五、Optional类
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/484371e74091404595e6a7320abb57b6.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/0e894798da51455aabe6ab9693d8cbba.png)
package com.gyh;
import java.util.Optional;
public class StreamAPITest7 {
public static void main(String[] args) {
Employee employee = new Employee("aa", 12, 1001, 9000);
Optional<Employee> employee1 = Optional.of(employee);
Optional<Object> empty = Optional.empty();
Optional<Employee> employee2 = Optional.ofNullable(employee);
}
}