基础示例类:
Use.java
public class User {
private String userId;
private String userName;
private Integer age;
private String region;
}
1.steam流遍历List列表
List<User> userList = new ArrayList<>();
userList.add(new User("1", "张三", 20, "上海"));
userList.add(new User("2", "李四", 20, "北京"));
userList.add(new User("3", "王五", 25, "上海"));
userList.stream().forEach(user -> {
// 操作
System.out.println(user.getUserName() + ":" + user.getAge() + ":" + user.getRegion());
});
// 与上面方法等同
userList.forEach(user -> {
// 操作
System.out.println(user.getUserName() + ":" + user.getAge() + ":" + user.getRegion());
});
2.steam流根据某个字段进行分组
例如需要对User里统一地区的人进行分组
List<User> userList = new ArrayList<>();
userList.add(new User("1", "张三", 20, "上海"));
userList.add(new User("2", "李四", 20, "北京"));
userList.add(new User("3", "王五", 25, "上海"));
Map<String, List<User>> collect = userList.stream().collect(Collectors.groupingBy(User::getRegion));
System.out.println(collect);
结果:
{
上海=[
User(userId=1, userName=张三, age=20, region=上海),
User(userId=3, userName=王五, age=25, region=上海)
],
北京=[
User(userId=2, userName=李四, age=20, region=北京)
]
}
3.steam流将list里面某个字段合并成String并用,隔开
例如获取User列表所有用户姓名
List<User> userList = new ArrayList<>();
userList.add(new User("1", "张三", 20, "上海"));
userList.add(new User("2", "李四", 20, "北京"));
userList.add(new User("3", "王五", 25, "上海"));
String userNameStr = userList.stream().map(User::getUserName).collect(Collectors.joining(","));
System.out.println(userNameStr);
结果:
张三,李四,王五
4.steam流 求和
例如,计算User列表所有用户的总年龄
List<User> userList = new ArrayList<>();
userList.add(new User("1", "张三", 20, "上海"));
userList.add(new User("2", "李四", 20, "北京"));
userList.add(new User("3", "王五", 25, "上海"));
int sumAge = userList.stream().mapToInt(User::getAge).sum();
结果:
sumAge=65
5.steam流 对某个数字字段排序,并转换为链表
List<User> userList = new ArrayList<>();
userList.add(new User("张三", 20));
userList.add(new User("李四", 27));
userList.add(new User("王五", 25));
userList.add(new User("赵六", 18));
System.out.println("排序前:");
System.out.println(userList);
// 根据年龄 升序排序 并转换结果为链表
LinkedList<User> collect = userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toCollection(LinkedList::new));
System.out.println("升序排序后:");
System.out.println(collect);
// 根据年龄 降序排序 并转换结果为链表
LinkedList<User> collect2 = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toCollection(LinkedList::new));
System.out.println("降序排序后:");
System.out.println(collect2);
结果:
排序前:
[User(name=张三, age=20), User(name=李四, age=27), User(name=王五, age=25), User(name=赵六, age=18)]
升序排序后:
[User(name=赵六, age=18), User(name=张三, age=20), User(name=王五, age=25), User(name=李四, age=27)]
降序排序后:
[User(name=李四, age=27), User(name=王五, age=25), User(name=张三, age=20), User(name=赵六, age=18)]
6.steam流 提取某个字段内容组成列表
List<User> userList = new ArrayList<>();
userList.add(new User("张三", 20));
userList.add(new User("李四", 27));
userList.add(new User("王五", 25));
userList.add(new User("赵六", 18));
// 提取用户姓名组成list
List<String> userNameList = userList.stream().map(User::getName).collect(Collectors.toList());
System.out.println(userNameList);
结果:
[张三, 李四, 王五, 赵六]