• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
自由的代价是孤独丶
博客园    首页    新随笔    联系   管理    订阅  订阅

stream流

import java.util.*;
import java.util.stream.Collectors;

public class UserCollectionExample {
public static void main(String[] args) {
// 创建一个用户集合
List users = Arrays.asList(
new User("张三", 20, "北京"),
new User("李四", 25, "上海"),
new User("王五", 30, "广州"),
new User("赵六", 35, "深圳"),
new User("孙七", 20, "北京")
);

// 过滤年龄大于25的用户
List filteredUsers = users.stream()
.filter(user -> user.getAge() > 25)
.collect(Collectors.toList());
// 输出过滤后的用户
filteredUsers.forEach(System.out::println);

// 映射用户的名字
List userNames = users.stream()
.map(User::getName)
.collect(Collectors.toList());
// 输出用户的名字
userNames.forEach(System.out::println);

// 对用户按年龄排序
List sortedUsers = users.stream()
.sorted(Comparator.comparingInt(User::getAge))
.collect(Collectors.toList());
// 输出排序后的用户
sortedUsers.forEach(System.out::println);

// 查找年龄为25的用户
Optional userOptional = users.stream()
.filter(user -> user.getAge() == 25)
.findFirst();
// 输出找到的用户
userOptional.ifPresent(System.out::println);

// 检查是否有来自广州的用户
boolean hasGuangzhouUser = users.stream()
.anyMatch(user -> user.getCity().equals("广州"));
// 输出检查结果
System.out.println("是否有来自广州的用户: " + hasGuangzhouUser);

// 获取所有用户的年龄列表
List userAges = users.stream()
.map(User::getAge)
.collect(Collectors.toList());
// 输出年龄列表
userAges.forEach(System.out::println);

// 使用reduce计算所有用户的总年龄
Optional totalAge = users.stream()
.map(User::getAge)
.reduce(Integer::sum);
// 输出总年龄
totalAge.ifPresent(age -> System.out.println("所有用户的总年龄: " + age));

// 按城市分组用户
Map<String, List> usersByCity = users.stream()
.collect(Collectors.groupingBy(User::getCity));
// 输出按城市分组的用户
usersByCity.forEach((city, userList) -> {
System.out.println(city + "😊;
userList.forEach(System.out::println);
});

// 获取所有用户的名字并合并成一个字符串
String allNames = users.stream()
.map(User::getName)
.collect(Collectors.joining(", "));
// 输出合并后的名字
System.out.println("所有用户的名字: " + allNames);

// 获取年龄大于25的用户数量
long countOfUsersAbove25 = users.stream()
.filter(user -> user.getAge() > 25)
.count();
// 输出数量
System.out.println("年龄大于25的用户数量: " + countOfUsersAbove25);

// 获取最小年龄的用户
Optional youngestUser = users.stream()
.min(Comparator.comparingInt(User::getAge));
// 输出最小年龄的用户
youngestUser.ifPresent(user -> System.out.println("最小年龄的用户: " + user));

// 获取最大年龄的用户
Optional oldestUser = users.stream()
.max(Comparator.comparingInt(User::getAge));
// 输出最大年龄的用户
oldestUser.ifPresent(user -> System.out.println("最大年龄的用户: " + user));

// 按城市分区用户
Map<Boolean, List> usersPartitionedByCity = users.stream()
.collect(Collectors.partitioningBy(user -> user.getCity().equals("北京")));
// 输出按城市分区的用户
usersPartitionedByCity.forEach((key, userList) -> {
System.out.println((key ? "来自北京" : "不来自北京") + "😊;
userList.forEach(System.out::println);
});
}
}

class User {
private String name;
private int age;
private String city;

public User(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getCity() {
return city;
}

@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
", city='" + city + ''' +
'}';
}
}

posted @ 2025-01-16 16:44  &emsp;不将就鸭  阅读(48)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3