Java 8 Lambda 表达式
- 1 Lambda 语法
- 2 Lambda 特征
- 3 Lambda 特点
- 4 Lambda sorted
-
- 4.1 Lambda sorted 排序
- 4.2 Lambda sorted 正序
- 4.3 Lambda sorted 逆序
- 4.4 Lambda sorted 多条件排序
- 5 Lambda filter
- 6 Lambda distinct
- 7 Lambda forEach
- 8 Lambda map
-
- 8.1 Lambda map 取集合
- 8.2 Lambda map 最大值
- 8.3 Lambda map 最小值
- 8.3 Lambda map 计数
- 8.3 Lambda map 求和
- 8.3 Lambda map 平均值
- 9 Lambda 其他用法
-
- 9.1 Lambda String[] 转 List
- 9.2 Lambda List 转 String[]
1 Lambda 语法
(parameters) -> expression
(parameters) ->{
statements; }
2 Lambda 特征
1 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
2 可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
3 可选的大括号:如果主体包含了一个语句,就不需要使用大括号。
4 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指定明表达式返回了一个数值。
3 Lambda 特点
1 Lambda 表达式主要用来定义行内执行的方法类型接口,例如,一个简单方法接口。在上面例子中,我们使用各种类型的Lambda表达式来定义MathOperation接口的方法。
2 Lambda 表达式免去了使用匿名方法的麻烦,并且给予Java简单但是强大的函数化的编程能力。
3 lambda 表达式只能引用标记了 final 的外层局部变量,这就是说不能在 lambda 内部修改定义在域外的局部变量,否则会编译错误。
4 Lambda sorted
4.1 Lambda sorted 排序
package com.xu.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
User user1 = new User();
user1.setAge(20);
user1.setName("张三");
User user2 = new User();
user2.setAge(21);
user2.setName("李四");
User user3 = new User();
user3.setAge(22);
user3.setName("王五");
List<User> list = Arrays.asList(user1, user2, user3);
list.stream().sorted(Comparator.comparing(User::getName).reversed().thenComparing(User::getAge))
.collect(Collectors.toList())
.forEach(e->System.out.println(e.toString()));
}
}
list.stream().sorted(Comparator.comparing(User::getName).reversed().thenComparing(User::getAge)).collect(Collectors.toList()).forEach(e->System.out.println(e.toString()));
User [age=20, sex=null, name=张三, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
4.2 Lambda sorted 正序
list.stream().sorted(Comparator.comparing(User::getName)).forEach(e->System.out.println(e.toString()));
User [age=20, sex=null, name=张三, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
4.3 Lambda sorted 逆序
list.stream().sorted(Comparator.comparing(User::getName).reversed()).forEach(e->System.out.println(e.toString()));
User [age=22, sex=null, name=王五, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=20, sex=null, name=张三, email=null]
4.4 Lambda sorted 多条件排序
list.stream().sorted(Comparator.comparing(User::getName).thenComparing(User::getAge)).forEach(e->System.out.println(e.toString()));
User [age=20, sex=null, name=张三, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
5 Lambda filter
package com.xu.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
User user1 = new User();
user1.setAge(20);
user1.setName("张三");
User user2 = new User();
user2.setAge(21);
user2.setName("李四");
User user3 = new User();
user3.setAge(22);
user3.setName("王五");
List<User> list = Arrays.asList(user1, user2, user3);
list.stream().filter(e->e.getAge()>=21).forEach(e->System.out.println(e.toString()));
}
}
list.stream().filter(e->e.getAge()>=21).forEach(e->System.out.println(e.toString()));
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
6 Lambda distinct
package com.xu.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
User user1 = new User();
user1.setAge(20);
user1.setName("张三");
User user2 = new User();
user2.setAge(21);
user2.setName("李四");
User user3 = new User();
user3.setAge(22);
user3.setName("王五");
List<User> list = Arrays.asList(user1, user2, user3);
list.stream().distinct().forEach(e->System.out.println(e.toString()));
}
}
list.stream().distinct().forEach(e->System.out.println(e.toString()));
User [age=20, sex=null, name=张三, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
7 Lambda forEach
package com.xu.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
User user1 = new User();
user1.setAge(20);
user1.setName("张三");
User user2 = new User();
user2.setAge(21);
user2.setName("李四");
User user3 = new User();
user3.setAge(22);
user3.setName("王五");List<User> list = Arrays.asList(user1, user1, user2, user3);
list.forEach(e->System.out.println(e.toString()));
}
}
list.forEach(e->System.out.println(e.toString()));
list.stream().forEach(e->System.out.println(e.toString()));
list.stream().forEach(System.out::println);
User [age=20, sex=null, name=张三, email=null]
User [age=20, sex=null, name=张三, email=null]
User [age=21, sex=null, name=李四, email=null]
User [age=22, sex=null, name=王五, email=null]
8 Lambda map
package com.xu.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String args[]) {
User user1 = new User();
user1.setAge(20);
user1.setName("张三");
User user2 = new User();
user2.setAge(21);
user2.setName("李四");
User user3 = new User();
user3.setAge(22);
user3.setName("王五");
List<User> list = Arrays.asList(user1, user2, user3);
list.stream().map(e->{
if(e.getName().equals("张三")) {
e.setAge(e.getAge()+10);
}
return e;
}).forEach(System.out::println);
}
}
list.stream().map(e->{
if(e.getName().equals("张三")) {
e.setAge(e.getAge()+10);
}
return e;
}).forEach(System.out::println);
8.1 Lambda map 取集合
list.stream().map(User::getAge).forEach(System.out::println);
20
21
22
8.2 Lambda map 最大值
int a = list.stream().map(User::getAge).max(Integer::compareTo).get();
System.out.println(a);
22
8.3 Lambda map 最小值
int a = list.stream().map(User::getAge).min(Integer::compareTo).get();
System.out.println(a);
20
8.3 Lambda map 计数
int a = list.stream().map(User::getAge).count();
System.out.println(a);
3
8.3 Lambda map 求和
int a = list.stream().map(User::getAge).sum();
System.out.println(a);
63
8.3 Lambda map 平均值
int a = list.stream().mapToInt(User::getAge).average();
System.out.println(a);
21
9 Lambda 其他用法
9.1 Lambda String[] 转 List
String[] strs = {
"a","b","c","d"};
List<String> list = Stream.of(strs).collect(Collectors.toList());
9.2 Lambda List 转 String[]
String[] strs = {
"a","b","c","d"};
List<String> list = Stream.of(strs).collect(Collectors.toList());
strs = list.stream().toArray(String[]::new);