Java8新特性
视频参考:尚硅谷-宋红康-bilibili(https://www.bilibili.com/video/BV1Kb411W75N?p=661)
博客参考:https://blog.csdn.net/PorkBird/article/details/113727704
用了周末两天时间看完了也进行了练习,学到了不少,老师讲的很细致~
一、Java8概述
- Java 8 (又称为jdk 1.8) 是Java 语言开发的一个主要版本。
- Java 8 是oracle公司于2014年3月发布,可以看成是自Java 5 以来最具革命性的版本。Java 8为Java语言、编译器、类库、开发工具与JVM带来了大量新特性。
二、Java8新特性的好处
- 速度更快
- 代码更少(增加了新的语法:Lambda 表达式)
- 强大的Stream API
- 便于并行
- 最大化减少空指针异常:Optional
- Nashorn引擎,允许在JVM上运行JS应用
三、并行流与串行流
并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流。相比较串行的流,并行的流可以很大程度上提高程序的执行效率。
Java 8 中将并行进行了优化,我们可以很容易的对数据进行并行操作。Stream API 可以声明性地通过parallel() 与sequential() 在并行流与顺序流之间进行切换。
四、Lambda表达式
Lambda 是一个匿名函数,我们可以把Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。使用它可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。
4.1、Lambda表达式使用举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Lambda表达式的使用举例 */ public class LambdaTest { @Test public void test() { Runnable r1 = new Runnable() { @Override public void run() { System.out.println( "长安欢迎您" ); } }; r1.run(); System.out.println( "+++++++++++++++++++++++++|" ); Runnable r2 = () -> System.out.println( "长安欢迎您" ); r2.run(); } @Test public void test2() { Comparator<Integer> c1 = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1, o2); } }; int compare1 = c1.compare( 8 , 16 ); System.out.println(compare1); System.out.println( "+++++++++++++++++++++++" ); //Lambda表达式的写法 Comparator<Integer> c2 = (o1, o2) -> Integer.compare(o1, o2); int compare2 = c2.compare( 28 , 35 ); System.out.println(compare2); System.out.println( "+++++++++++++++++++++++++++" ); //方法引用 Comparator<Integer> c3 = Integer::compare; int compare3 = c3.compare( 28 , 35 ); System.out.println(compare3); } } |
4.2、Lambda表达式语法使用1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | /** * Lambda表达式的使用 * <p> * 1.举例: (o1,o2) -> Integer.compare(o1,o2); * 2.格式: * -> :lambda操作符 或 箭头操作符 * ->左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表) * ->右边:lambda体 (其实就是重写的抽象方法的方法体) * <p> * 3.Lambda表达式的使用:(分为6种情况介绍) */ @SuppressWarnings ( "all" ) public class LambdaTest1 { //语法格式一:无参,无返回值 @Test public void test() { Runnable r1 = new Runnable() { @Override public void run() { System.out.println( "长安欢迎您" ); } }; r1.run(); System.out.println( "++++++++++使用Lambda的写法++++++++++++++|" ); Runnable r2 = () -> System.out.println( "长安欢迎您" ); r2.run(); } //语法格式二:Lambda 需要一个参数,但是没有返回值。 @Test public void test2() { Consumer<String> con = new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }; con.accept( "善与恶的区别是什么?" ); System.out.println( "+++++++++++++++++++" ); Consumer<String> c1 = (String s) -> { System.out.println(s); }; c1.accept( "先天人性无善恶,后天人性有善恶。" ); } //语法格式三:数据类型可以省略,因为可由编译器推断得出,称为“类型推断” @Test public void test3() { Consumer<String> c1 = (String s) -> { System.out.println(s); }; c1.accept( "先天人性无善恶,后天人性有善恶。" ); System.out.println( "---------------------" ); Consumer<String> c2 = (s) -> { System.out.println(s); }; c2.accept( "如果没有邪恶的话我们怎么会知道人世间的那些善良呢?" ); } @Test public void test4() { ArrayList<String> list = new ArrayList<>(); //类型推断 int [] arr = { 1 , 2 , 3 }; //类型推断 } } |
4.3、Lambda表达式语法使用2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import org.junit.Test; import java.util.Comparator; import java.util.function.Consumer; /** * Lambda表达式的使用 * * 1.举例: (o1,o2) -> Integer.compare(o1,o2); * 2.格式: * -> :lambda操作符 或 箭头操作符 * ->左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表) * ->右边:lambda体 (其实就是重写的抽象方法的方法体) * * 3.Lambda表达式的使用:(分为6种情况介绍) * * 总结: * ->左边:lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只有一个参数,其一对()也可以省略 * ->右边:lambda体应该使用一对{}包裹;如果lambda体只有一条执行语句(可能是return语句),省略这一对{}和return关键字 */ public class LambdaTest1 { //语法格式四:Lambda若只需要一个参数时,参数的小括号可以省略 @Test public void test5(){ Consumer<String> c1 = (s) -> { System.out.println(s); }; c1.accept( "先天人性无善恶,后天人性有善恶。" ); System.out.println( "---------------------" ); Consumer<String> c2 = s -> { System.out.println(s); }; c2.accept( "如果没有邪恶的话我们怎么会知道人世间的那些善良呢?" ); } //语法格式五:Lambda需要两个或以上的参数,多条执行语句,并且可以有返回值 @Test public void test6(){ Comparator<Integer> c1 = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { System.out.println(o1); System.out.println(o2); return o1.compareTo(o2); } }; System.out.println(c1.compare( 15 , 23 )); System.out.println( "\\\\\\\\\\\\\\\\\\\\\\\\\\" ); Comparator<Integer> com2 = (o1,o2) -> { System.out.println(o1); System.out.println(o2); return o1.compareTo(o2); }; System.out.println(com2.compare( 16 , 8 )); } //语法格式六:当Lambda体只有一条语句时,return与大括号若有,都可以省略 @Test public void test7(){ Comparator<Integer> c1 = (o1,o2) -> { return o1.compareTo(o2); }; System.out.println(c1.compare( 16 , 8 )); System.out.println( "\\\\\\\\\\\\\\\\\\\\\\\\\\" ); Comparator<Integer> c2 = (o1,o2) -> o1.compareTo(o2); System.out.println(c2.compare( 17 , 24 )); } @Test public void test8(){ Consumer<String> c1 = s -> { System.out.println(s); }; c1.accept( "先天人性无善恶,后天人性有善恶。" ); System.out.println( "---------------------" ); Consumer<String> c2 = s -> System.out.println(s); c2.accept( "如果没有邪恶的话我们怎么会知道人世间的那些善良呢?" ); } } |
五、函数式接口的使用
5.1、函数式接口的介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /* * 自定义函数式接口 * 1.Lambda表达式的本质:作为函数式接口的实例 * * 2. 如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口。我们可以在一个接口上使用 @FunctionalInterface 注解, * 这样做可以检查它是否是一个函数式接口。 * */ @FunctionalInterface interface MyInterFace { void method(); // void method2(); } |
- 在java.util.function包下定义了Java 8 的丰富的函数式接口
- Java从诞生日起就是一直倡导“一切皆对象”,在Java里面面向对象(OOP)编程是一切。但是随着python、scala等语言的兴起和新技术的挑战,Java不得不做出调整以便支持更加广泛的技术要求,也即java不但可以支持OOP还可以支持OOF(面向函数编程)
- 在函数式编程语言当中,函数被当做一等公民对待。在将函数作为一等公民的编程语言中,Lambda表达式的类型是函数。但是在Java8中,有所不同。在Java8中,Lambda表达式是对象,而不是函数,它们必须依附于一类特别的对象类型——函数式接口。
- 简单的说,在Java8中,Lambda表达式就是一个函数式接口的实例。这就是Lambda表达式和函数式接口的关系。也就是说,只要一个对象是函数式接口的实例,那么该对象就可以用Lambda表达式来表示。
- 所以以前用匿名实现类表示的现在都可以用Lambda表达式来写。
5.2、Java内置的函数式接口介绍及使用举例
函数式接口 | 参数类型 | 返回值类型 | 用途 |
Consumer:消费型接口 | T | void | 对类型为T的对象应用操作,包含方法:void accept(T t) |
Supplier:供给型接口 | 无 | T | 返回类型为T的对象,包含方法:T get() |
Function<T,R>:函数型接口 | T | R | 对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法:R apply(T t) |
Predicate:断定型接口 | T | boolean | 确定类型为T的对象是否满足某约束,并返回boolean 值。包含方法:boolean test(T t) |
BiFunction<T,U,R> | T,U | R | 对类型为T,U参数应用操作,返回R类型的结果。包含方法为:Rapply(T t,U u) ; |
UnaryOperator(Function子接口) | T | T | 对类型为T的对象进行一元运算,并返回T类型的结果。包含方法为:Tapply(T t) ; |
BinaryOperator(BiFunction子接口) | T,T | T | 对类型为T的对象进行二元运算,并返回T类型的结果。包含方法为:Tapply(T t1,T t2) ; |
BiConsumer(T,U) | T,U | void | 对类型为T,U参数应用操作。包含方法为:void accept(Tt,Uu) |
BiPredicate<T,U> | T,U | boolean | 包含方法为:boolean test(Tt,Uu) |
ToLongFunction | T | long | 计算long值的函数 |
ToDoubleFunction | T | double | 计算double值的函数 |
ToIntFunction | T | int | 计算lnt值的函数 |
IntFunction | int | R | 参数为int 类型的函数 |
LoingFunction | long | R | 参数为iong 类型的函数 |
DoubleFunction | double | R | 参数为double类型的函数 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /** * java内置的4大核心函数式接口 * <p> * 消费型接口 Consumer<T> void accept(T t) * 供给型接口 Supplier<T> T get() * 函数型接口 Function<T,R> R apply(T t) * 断定型接口 Predicate<T> boolean test(T t) */ public class LambdaTest2 { /** * 消费型接口 * @param money 消费的数据 * @param con 执行消费的数据 */ public void happyTime( double money, Consumer<Double> con) { con.accept(money); } /** * 根据给定的规则,过滤集合中的字符串。此规则由Predicate的方法决定 * @param list 要处理的数据 * @param pre 断定接口 * @return 返回处理后的数据 */ public List<String> filterString(List<String> list, Predicate<String> pre) { ArrayList<String> filterList = new ArrayList<>(); for (String s : list) { if (pre.test(s)) { filterList.add(s); } } return filterList; } @Test public void test() { happyTime( 30 , new Consumer<Double>() { @Override public void accept(Double aDouble) { System.out.println( "熬夜太累了,点个外卖,价格为:" + aDouble); } }); System.out.println( "+++++++++++++++++++++++++" ); /*Lambda表达式写法*/ happyTime( 20 , money -> System.out.println( "熬夜太累了,吃口麻辣烫,价格为:" + money)); } @Test public void test2() { List<String> list = Arrays.asList( "长安" , "上京" , "江南" , "渝州" , "凉州" , "兖州" ); /*判断集合中包含州的地域*/ List<String> filterStrs = filterString(list, new Predicate<String>() { @Override public boolean test(String s) { return s.contains( "州" ); } }); System.out.println(filterStrs); /*判断集合中包含州的地域的Lambda写法*/ List<String> filterStrs1 = filterString(list, s -> s.contains( "州" )); System.out.println(filterStrs1); } } |
六、方法引用与构造器引用
当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
方法引用可以看做是Lambda表达式深层次的表达。换句话说,方法引用就是Lambda表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是Lambda表达式的一个语法糖。
要求:
实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!
格式:
使用操作符“::” 将类(或对象) 与方法名分隔开来。
如下三种主要使用情况:
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
6.1、方法引用使用1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /** * 方法引用的使用 * <p> * 1.使用情境:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用! * <p> * 2.方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以 * 方法引用,也是函数式接口的实例。 * <p> * 3. 使用格式: 类(或对象) :: 方法名 * <p> * 4. 具体分为如下的三种情况: * 情况1 对象 :: 非静态方法 * 情况2 类 :: 静态方法 * <p> * 情况3 类 :: 非静态方法 * <p> * 5. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的 * 形参列表和返回值类型相同!(针对于情况1和情况2) */ public class MethodRefTest { /** * 情况一:对象 :: 实例方法 * Consumer中的void accept(T t) * PrintStream中的void println(T t) */ @Test public void test() { Consumer<String> c1 = str -> System.out.println(str); c1.accept( "兖州" ); System.out.println( "+++++++++++++" ); PrintStream ps = System.out; Consumer<String> c2 = ps::println; c2.accept( "xian" ); } /** * Supplier中的T get() * Employee中的String getName() */ @Test public void test2() { Employee emp = new Employee( 004 , "Nice" , 19 , 4200 ); Supplier<String> sk1 = () -> emp.getName(); System.out.println(sk1.get()); System.out.println( "*******************" ); Supplier<String> sk2 = emp::getName; System.out.println(sk2.get()); } } class Employee { private int id; private String name; private int age; private double salary; public int getId() { return id; } public void setId( int id) { this .id = id; } 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 double getSalary() { return salary; } public void setSalary( double salary) { this .salary = salary; } public Employee() { System.out.println( "Employee()....." ); } public Employee( int id) { this .id = id; System.out.println( "Employee(int id)....." ); } public Employee( int id, String name) { this .id = id; this .name = name; } public Employee( int id, String name, int age, double salary) { this .id = id; this .name = name; this .age = age; this .salary = salary; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\ '' + ", age=" + age + ", 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; if (id != employee.id) { return false ; } if (age != employee.age) { return false ; } if (Double.compare(employee.salary, salary) != 0 ) { return false ; } return name != null ? name.equals(employee.name) : employee.name == null ; } @Override public int hashCode() { int result; long temp; result = id; result = 31 * result + (name != null ? name.hashCode() : 0 ); result = 31 * result + age; temp = Double.doubleToLongBits(salary); result = 31 * result + ( int ) (temp ^ (temp >>> 32 )); return result; } } |
6.2、方法引用使用2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * 情况二:类 :: 静态方法 * Comparator中的int compare(T t1,T t2) * Integer中的int compare(T t1,T t2) */ @Test public void test3() { Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1, t2); System.out.println(com1.compare( 21 , 20 )); System.out.println( "+++++++++++++++" ); Comparator<Integer> com2 = Integer::compare; System.out.println(com2.compare( 15 , 7 )); } /** * Function中的R apply(T t) * Math中的Long round(Double d) */ @Test public void test4() { Function<Double, Long> func = new Function<Double, Long>() { @Override public Long apply(Double d) { return Math.round(d); } }; System.out.println( "++++++++++++++++++" ); Function<Double, Long> func1 = d -> Math.round(d); System.out.println(func1.apply( 14.1 )); System.out.println( "++++++++++++++++++" ); Function<Double, Long> func2 = Math::round; System.out.println(func2.apply( 17.4 )); } |
6.3、方法引用使用3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** * 情况三:类 :: 实例方法 (有难度) * Comparator中的int comapre(T t1,T t2) * String中的int t1.compareTo(t2) */ @Test public void test5() { Comparator<String> com1 = (s1, s2) -> s1.compareTo(s2); System.out.println(com1.compare( "abc" , "abd" )); System.out.println( "++++++++++++++++" ); Comparator<String> com2 = String::compareTo; System.out.println(com2.compare( "abd" , "abm" )); } /** * BiPredicate中的boolean test(T t1, T t2); * String中的boolean t1.equals(t2) */ @Test public void test6() { BiPredicate<String, String> pre1 = (s1, s2) -> s1.equals(s2); System.out.println(pre1.test( "MON" , "MON" )); System.out.println( "++++++++++++++++++++" ); BiPredicate<String, String> pre2 = String::equals; System.out.println(pre2.test( "MON" , "MON" )); } /** * Function中的R apply(T t) * Employee中的String getName(); */ @Test public void test7() { Employee employee = new Employee( 007 , "Ton" , 21 , 8000 ); Function<Employee, String> func1 = e -> e.getName(); System.out.println(func1.apply(employee)); System.out.println( "++++++++++++++++++++++++" ); Function<Employee, String> f2 = Employee::getName; System.out.println(f2.apply(employee)); } |
6.4、数组引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /*************************************构造器引用与数组引用****************************************************/ /** * 构造器引用 * Supplier中的T get() * Employee的空参构造器:Employee() */ @Test public void test8() { Supplier<Employee> sup = new Supplier<Employee>() { @Override public Employee get() { return new Employee(); } }; System.out.println( "+++++++++++++++++++" ); /*Lambda表达式写法*/ Supplier<Employee> sk1 = () -> new Employee(); System.out.println(sk1.get()); System.out.println( "+++++++++++++++++++" ); /*构造器引用写法*/ Supplier<Employee> sk2 = Employee:: new ; System.out.println(sk2.get()); } /**Function中的R apply(T t)*/ @Test public void test9() { Function<Integer, Employee> f1 = id -> new Employee(id); Employee employee = f1.apply( 7793 ); System.out.println(employee); System.out.println( "+++++++++++++++++++" ); /*构造器引用写法*/ Function<Integer, Employee> f2 = Employee:: new ; Employee employee1 = f2.apply( 4545 ); System.out.println(employee1); } /*BiFunction中的R apply(T t,U u)*/ @Test public void test10() { BiFunction<Integer, String, Employee> f1 = (id, name) -> new Employee(id, name); System.out.println(f1.apply( 2513 , "Fruk" )); System.out.println( "*******************" ); /*构造器引用写法*/ BiFunction<Integer, String, Employee> f2 = Employee:: new ; System.out.println(f2.apply( 9526 , "Bon" )); } /** * 数组引用 * Function中的R apply(T t) */ @Test public void test11() { Function<Integer, String[]> f1 = length -> new String[length]; String[] arr1 = f1.apply( 7 ); System.out.println(Arrays.toString(arr1)); System.out.println( "+++++++++++++++++++" ); /*数组引用写法*/ Function<Integer, String[]> f2 = String[]:: new ; String[] arr2 = f2.apply( 9 ); System.out.println(Arrays.toString(arr2)); } |
七、Stream-AIP
7.1、StreamAPI概述
Java8中有两大最为重要的改变。第一个是Lambda 表达式;另外一个则是Stream API。
Stream API ( java.util.stream)把真正的函数式编程风格引入到Java中。这是目前为止对Java类库最好的补充,因为Stream API可以极大提供Java程序员的生产力,让程序员写出高效率、干净、简洁的代码。
Stream 是Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,就类似于使用SQL 执行的数据库查询。
也可以使用Stream API 来并行执行操作。简言之,Stream API 提供了一种高效且易于使用的处理数据的方式。
为什么要使用Stream API
实际开发中,项目中多数数据源都来自于Mysql,Oracle等。但现在数据源可以更多了,有MongDB,Radis等,而这些NoSQL的数据就需要Java层面去处理。
Stream 和Collection 集合的区别:
Collection 是一种静态的内存数据结构,而Stream 是有关计算的。前者是主要面向内存,存储在内存中,后者主要是面向CPU,通过CPU 实现计算。
7.2、Stream的实例化
1、初识数据准备:
实体类

/** * @author zhangzhixi * @version 1.0 * @date 2021-11-15 14:36 */ public class Employee { private int id; private String name; private int age; private double salary; public int getId() { return id; } public void setId(int id) { this.id = id; } 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 double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Employee() { System.out.println("Employee()....."); } public Employee(int id) { this.id = id; System.out.println("Employee(int id)....."); } public Employee(int id, String name) { this.id = id; this.name = name; } public Employee(int id, String name, int age, double salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", 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; if (id != employee.id) return false; if (age != employee.age) return false; if (Double.compare(employee.salary, salary) != 0) return false; return name != null ? name.equals(employee.name) : employee.name == null; } @Override public int hashCode() { int result; long temp; result = id; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + age; temp = Double.doubleToLongBits(salary); result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } }
测试数据

/** * @author zhangzhixi * @version 1.0 * @date 2021-11-15 14:36 */ import java.util.ArrayList; import java.util.List; /** * 提供用于测试的数据 */ public class EmployeeData { public static List<Employee> getEmployees() { List<Employee> list = new ArrayList<>(); list.add(new Employee(1001, "马化腾", 34, 6000.38)); list.add(new Employee(1002, "马云", 12, 9876.12)); list.add(new Employee(1003, "刘强东", 33, 3000.82)); list.add(new Employee(1004, "雷军", 26, 7657.37)); list.add(new Employee(1005, "李彦宏", 65, 5555.32)); list.add(new Employee(1006, "比尔盖茨", 42, 9500.43)); list.add(new Employee(1007, "任正非", 26, 4333.32)); list.add(new Employee(1008, "扎克伯格", 35, 2500.32)); return list; } }
2、实例化测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.util.Arrays; import java.util.List; import java.util.stream.IntStream; import java.util.stream.Stream; /** * 测试Stream的实例化 */ @SuppressWarnings ( "all" ) public class StreamAPITest { /** * 创建 Stream方式一:通过集合 */ @Test public void test() { List<Employee> employees = EmployeeData.getEmployees(); // default Stream<E> stream() : 返回一个顺序流 Stream<Employee> stream = employees.stream(); // default Stream<E> parallelStream() : 返回一个并行流 Stream<Employee> parallelStream = employees.parallelStream(); } /** * 创建 Stream方式二:通过数组 */ @Test public void test2() { int [] arr = new int []{ 1 , 2 , 3 , 4 , 5 , 6 }; //调用Arrays类的static <T> Stream<T> stream(T[] array): 返回一个流 IntStream stream = Arrays.stream(arr); Employee e1 = new Employee( 1001 , "Hom" ); Employee e2 = new Employee( 1002 , "Nut" ); Employee[] arr1 = new Employee[]{e1, e2}; Stream<Employee> stream1 = Arrays.stream(arr1); } /** * 创建 Stream方式三:通过Stream的of() */ @Test public void test3() { Stream<Integer> stream = Stream.of( 1 , 2 , 3 , 4 , 5 , 6 ); stream.forEach(System.out::println); } //创建 Stream方式四:创建无限流 @Test public void test4() { // 迭代 // public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) //遍历前10个偶数 Stream.iterate( 0 , t -> t + 2 ).limit( 10 ).forEach(System.out::println); // 生成 // public static<T> Stream<T> generate(Supplier<T> s) Stream.generate(Math::random).limit( 10 ).forEach(System.out::println); } } |
7.3、Stream的中间操作:筛选与切片
多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为“惰性求值”。
方法 | 描述 |
filter(Predicate p) | 接收Lambda ,从流中排除某些元素 |
distinct() | 筛选,通过流所生成元素的hashCode() 和equals() 去除重复元素 |
limit(long maxSize) | 截断流,使其元素不超过给定数量 |
skip(long n) | 跳过元素,返回一个扔掉了前n 个元素的流。若流中元素不足n 个,则返回一个空流。与limit(n) 互补 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import org.junit.Test; import java.util.List; import java.util.stream.Stream; /** * 测试Stream的中间操作 */ public class StreamAPITest2 { //1-筛选与切片 @Test public void test() { List<Employee> list = EmployeeData.getEmployees(); /*filter(Predicate p)——接收 Lambda , 从流中排除某些元素。*/ Stream<Employee> stream = list.stream(); /*练习:查询员工表中薪资大于7000的员工信息*/ stream.filter(e -> e.getSalary() > 7000 ).forEach(System.out::println); System.out.println( "+++++++++++++++++++++++" ); /*limit(n)——截断流,使其元素不超过给定数量。*/ list.stream().limit( 3 ).forEach(System.out::println); System.out.println( "+++++++++++++++++++++++" ); /*skip(n) —— 跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补*/ list.stream().skip( 3 ).forEach(System.out::println); System.out.println( "+++++++++++++++++++++++" ); /*distinct()——筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素*/ list.add( new Employee( 1013 , "李飞" , 42 , 8500 )); list.add( new Employee( 1013 , "李飞" , 41 , 8200 )); list.add( new Employee( 1013 , "李飞" , 28 , 6000 )); list.add( new Employee( 1013 , "李飞" , 39 , 7800 )); list.add( new Employee( 1013 , "李飞" , 40 , 8000 )); // System.out.println(list); System.out.println( "===========" ); list.stream().distinct().forEach(System.out::println); } } |
7.4、Stream的中间操作:映射
方法 | 描述 |
map(Function f) | 接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。 |
mapToDouble(ToDoubleFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的DoubleStream。 |
mapToInt(ToIntFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的IntbleStream。 |
mapToLong(ToLongFunction f) | 接收一个函数作为参数,该函数会被应用到每个元素上,产生一个新的LongStream。 |
flatMap(Function f) | 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //2-映射 @Test public void test2() { // map(Function f)——接收一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每个元素上,并将其映射成一个新的元素。 List<String> list = Arrays.asList( "aa" , "bb" , "cc" , "dd" ); list.stream().map(str -> str.toUpperCase()).forEach(System.out::println); // 练习1:获取员工姓名长度大于3的员工的姓名。 List<Employee> employees = EmployeeData.getEmployees(); Stream<String> namesStream = employees.stream().map(Employee::getName); namesStream.filter(name -> name.length() > 3 ).forEach(System.out::println); System.out.println(); //练习2: Stream<Stream<Character>> streamStream = list.stream().map(StreamAPITest2::fromStringToStream); streamStream.forEach(s -> { s.forEach(System.out::println); }); System.out.println( "++++++++++++++++++++++" ); // flatMap(Function f)——接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。 Stream<Character> characterStream = list.stream().flatMap(StreamAPITest2::fromStringToStream); characterStream.forEach(System.out::println); } //将字符串中的多个字符构成的集合转换为对应的Stream的实例 public static Stream<Character> fromStringToStream(String str) { //aa ArrayList<Character> list = new ArrayList<>(); for (Character c : str.toCharArray()) { list.add(c); } return list.stream(); } |
7.5、Stream的中间操作:排序
方法 | 描述 |
sorted() | 产生一个新流,其中按自然顺序排序 |
sorted(Comparator com) | 产生一个新流,其中按比较器顺序排序 |
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Test public void test4() { /*1、准备数据*/ Integer[] integer = { 3 , 1 , 6 , 2 , 5 }; /*2、包装成流*/ Stream<Integer> stream = Arrays.stream(integer); /*3、进行排序:传入比较器*/ Stream<Integer> sorted = stream.sorted((o2, o1) -> o1.compareTo(o2)); /*4、遍历数据*/ sorted.forEach(System.out::println); System.out.println( "====自然排序:从小到大=========" ); Arrays.stream(integer).sorted().forEach(System.out::println); } |
7.6、Stream的终止操作:匹配与查找
方法 | 描述 |
allMatch(Predicate p) | 检查是否匹配所有元素 |
anyMatch(Predicate p) | 检查是否至少匹配一个元素 |
noneMatch(Predicate p) | 检查是否没有匹配所有元素 |
findFirst() | 返回第一个元素 |
findAny() | 返回当前流中的任意元素 |
count() | 返回流中元素总数 |
max(Comparator c) | 返回流中最大值 |
min(Comparator c) | 返回流中最小值 |
forEach(Consumer c) | 内部迭代(使用Collection 接口需要用户去做迭代,称为外部迭代。相反,Stream API 使用内部迭代——它帮你把迭代做了) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | //1-匹配与查找 @Test public void test(){ List<Employee> employees = EmployeeData.getEmployees(); // allMatch(Predicate p)——检查是否匹配所有元素。 // 练习:是否所有的员工的年龄都大于23 boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 23 ); System.out.println(allMatch); // anyMatch(Predicate p)——检查是否至少匹配一个元素。 // 练习:是否存在员工的工资大于 10000 boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 9000 ); System.out.println(anyMatch); // noneMatch(Predicate p)——检查是否没有匹配的元素。 // 练习:是否存在员工姓“马” boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith( "马" )); System.out.println(noneMatch); // findFirst——返回第一个元素 Optional<Employee> employee = employees.stream().findFirst(); System.out.println(employee); // findAny——返回当前流中的任意元素 Optional<Employee> employee1 = employees.parallelStream().findAny(); System.out.println(employee1); } @Test public void test2(){ List<Employee> employees = EmployeeData.getEmployees(); // count——返回流中元素的总个数 long count = employees.stream().filter(e -> e.getSalary() > 4500 ).count(); System.out.println(count); // max(Comparator c)——返回流中最大值 // 练习:返回最高的工资: Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary()); Optional<Double> maxSalary = salaryStream.max(Double::compare); System.out.println(maxSalary); // min(Comparator c)——返回流中最小值 // 练习:返回最低工资的员工 Optional<Employee> employee = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(employee); System.out.println(); // forEach(Consumer c)——内部迭代 employees.stream().forEach(System.out::println); //使用集合的遍历操作 employees.forEach(System.out::println); } |
7.7、Stream的终止操作:归约
方法 | 描述 |
reduce(T iden, BinaryOperator b) | 可以将流中元素反复结合起来,得到一个值。返回T |
reduce(BinaryOperator b) | 可以将流中元素反复结合起来,得到一个值。返回Optional |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //2-归约 @Test public void test3() { // reduce(T identity, BinaryOperator)——可以将流中元素反复结合起来,得到一个值。返回 T // 练习1:计算1-10的自然数的和 List<Integer> list = Arrays.asList( 72 , 25 , 32 , 34 , 43 , 56 , 81 , 15 , 29 , 71 ); Integer sum = list.stream().reduce( 0 , Integer::sum); System.out.println(sum); // reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。返回 Optional<T> // 练习2:计算公司所有员工工资的总和 List<Employee> employees = EmployeeData.getEmployees(); Stream<Double> salaryStream = employees.stream().map(Employee::getSalary); // Optional<Double> sumMoney = salaryStream.reduce(Double::sum); Optional<Double> sumMoney = salaryStream.reduce((d1, d2) -> d1 + d2); System.out.println(sumMoney.get()); } |
7.8、Stream的终止操作:收集
方法 | 描述 |
collect(Collector c) | 将流转换为其他形式。接收一个Collector接口的实现,用于给Stream中元素做汇总的方法 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //3-收集 @Test public void test4() { // collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法 // 练习1:查找工资大于6000的员工,结果返回为一个List或Set List<Employee> employees = EmployeeData.getEmployees(); List<Employee> employeeList = employees.stream().filter(e -> e.getSalary() > 6000 ).collect(Collectors.toList()); employeeList.forEach(System.out::println); System.out.println( "++++++++++++++++++" ); Set<Employee> employeeSet = employees.stream().filter(e -> e.getSalary() > 6000 ).collect(Collectors.toSet()); employeeSet.forEach(System.out::println); } |
Collector
接口中方法的实现决定了如何对流执行收集的操作(如收集到List、Set、Map
)。
Collectors
实用类提供了很多静态方法,可以方便地创建常见收集器实例,具体方法与实例如下表:
示例:
Collectors.join():用于将流中元素拼接成字符串
1 2 3 4 5 6 | public static void main(String[] args) { List<String> nameStrs = Arrays.asList( "张三" , "李四" , "王五" ); System. out .println(nameStrs.stream().collect(Collectors.joining())); // 张三李四王五 System. out .println(nameStrs.stream().collect(Collectors.joining( "-" ))); // 张三-李四-王五 System. out .println(nameStrs.stream().collect(Collectors.joining( "," , "[" , "]" ))); // [张三,李四,王五] } |
Collectors.mapping()
Collectors.mapping()
是 Java 8 中 java.util.stream.Collectors
类的一个静态方法,它用于在流处理中应用一个函数到每个元素上,然后将结果收集到一个新的流中。
这个方法通常与其他收集器(如 toList()
、toSet()
或 reducing()
等)配合使用,以实现更复杂的集合转换和聚合操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Test { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add( new Person( "Ram" , 30)); personList.add( new Person( "Shyam" , 20)); personList.add( new Person( "Shiv" , 20)); personList.add( new Person( "Mahesh" , 30)); // ====================================Collectors.mapping和Collectors.joining====================================== String nameByAge = personList.stream().collect(Collectors.mapping(Person::getName, Collectors.joining( "," , "[" , "]" ))); System. out .println(nameByAge); // [Ram,Shyam,Shiv,Mahesh] nameByAge = personList.stream().map(Person::getName).collect(Collectors.joining( "," , "[" , "]" )); System. out .println(nameByAge); // [Ram,Shyam,Shiv,Mahesh] // ======================================================================================================= // 按照年龄分组 Map<Integer, List<Person>> personGroupMap = personList.stream().collect(Collectors.groupingBy(Person::getAge)); System. out .println(personGroupMap); // {20=[Person(name=Shyam, age=20), Person(name=Shiv, age=20)], 30=[Person(name=Ram, age=30), Person(name=Mahesh, age=30)]} // 按照年龄分组,只取姓名 Map<Integer, String> personGroupName = personList.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.mapping(Person::getName, Collectors.joining( "," , "[" , "]" )))); System. out .println(personGroupName); // {20=[Shyam,Shiv], 30=[Ram,Mahesh]} } } @Data @NoArgsConstructor @AllArgsConstructor class Person { private String name; private int age; } |
八、Optional
8.1、什么是Optional
Optional
是 Java 中的一个类,它是在 Java 8 中引入的,用于解决空指针异常(NullPointerException)的问题。
Optional
的目标是通过强制显式处理可能为空的值,从而减少代码中的空指针异常。
8.2、使用Optional
1、创建Optional对象
Optional.ofNullable(T t):创建一个 Optional 实例,如果 t 的值不为 null,则具有当前值,否则为空 Optional
Optional.of(T t):创建一个 Optional 实例,当 t为null时抛出异常
2、判断是否为null
ifPresent(Consumer c):如果optional不为空,则将optional中的对象传给Comsumer函数,否则(为空)不执行任何操作
3、处理值
orElse(T t):如果存在,则返回该值,否则返回 other
orElseThrow(Supplier exceptionSupplier):如果存在,则返回该值,否则抛出异常
orElseGet(Supplier other):如果存在,则返回该值,否则返回other,并返回other调用结果
1 2 3 4 5 6 7 8 9 10 11 12 | // orElse:如果存在,则返回值,否则返回 other String str1 = null ; String str1Vla = Optional.ofNullable(str1).orElse( "other" ); System. out .println(str1Vla); // other // orElseThrow:如果存在,则返回该值,否则抛出异常 String str2 = "hello" ; String str2Vla = Optional.ofNullable(str2).orElseThrow(() -> new RuntimeException( "str2不能为空" )); System. out .println(str2Vla); // hello // orElseGet:如果存在,则返回该值,否则返回other,并返回other调用结果 String str3 = null ; String str3Value = Optional.ofNullable(str3).orElseGet(() -> "zhixi" ); System. out .println(str3Value); // zhixi |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话