文章目录
经常用stream操作,但一段时间不用就会忘记,借此来写篇博客~
List与Map转换
List 转换为类似为 Map<Integer, List>
按某个属性进行分组
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取名字相同的记录,拼接成map
Map<String, User> map = list.stream().collect(
Collectors.groupingBy(User::getName));
}
List 转换为类似为 Map<Integer, Object>
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//防止key值冲突
Map<String, User> map = list.stream().collect(Collectors.toMap(
e -> {
return (e.getName() + "_" + e.getAge());
}, Function.identity(), (key1, key2) -> key2)
);
}
此处有两种写法:
- Collectors.toMap(User::getName, User -> User);
- Collectors.toMap(User::getName, Function.identity());
List 转换为类似为 Map<属性1, 属性2>
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//两个属性值,一个为key,一个为value
Map<String, Integer> map = list.stream().collect(
Collectors.toMap(User::getName, User::getAge));
}
List<对象>转List<属性>
例子中用到的User实体类
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
提取对象中单个属性的集合or去重
如下案例,提取User对象
集合中属性name
集合
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取name集合
List<String> nameList = list.stream().map(User::getName).collect(Collectors.toList());
//提取name集合并去重,distinct()
List<String> nameList = list.stream().map(User::getName).distinct().collect(Collectors.toList());
}
按属性分组to Map
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取name集合
Map<String, List<User>> ageMap = list.stream().collect(
Collectors.groupingBy(User::getAge));
}
过滤某个属性,返回一个值
举个栗子:从列表中获取年龄为16岁的某个用户姓名(随机)
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class TestService {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
User user1 = new User("lizzy", 18);
userList.add(user1);
User user2 = new User("小芳", 16);
userList.add(user2);
User user3 = new User("小花", 17);
userList.add(user3);
User user4 = new User("小玲", 19);
userList.add(user4);
User user5 = new User("小雨", 20);
userList.add(user5);
Optional<String> name = userList.stream().filter(user -> user.getAge() == 16).map(User::getName).findFirst();
if (name.isPresent()) {
System.out.println(String.format("年龄16岁的小姐姐芳名为:%s", name.get()));
}
}
}
List某字段求和/取平均等操作
IntSummaryStatistics、相应的还有DoubleSummaryStatistics, LongSummaryStatistics
public class SummaryStatistics {
public static void main(String[] args) {
//1.对int类型list进行求和
List<User> list = new ArrayList<User>();
list.add(new User('lizzy', 18));
list.add(new User('huajuan', 28));
IntSummaryStatistics summaryStatistics = list.stream().mapToInt((s) -> s.getAge).summaryStatistics();
System.out.println("总和:" + summaryStatistics.getSum());
System.out.println("平均数:" + summaryStatistics.getAverage());
System.out.println("总个数:" + summaryStatistics.getCount());
System.out.println("最大值:" + summaryStatistics.getMax());
System.out.println("最小值:" + summaryStatistics.getMin());
}
}
踩过的坑
foreach无序
java stream 有序_关于java:Stream.forEach是否遵循顺序流的遭遇顺序?
java foreach顺序_面试:Stream#foreach方法摸底三问,你都了解吗
需要在并行流中保证元素顺序消费需要使用java.util.stream.Stream#forEachOrdered
未完待续。。。