java 8 流的操作

62 阅读1分钟

java 8 流常见的操作

// 过滤出性别为男的用户
List<User> userListMan=userList.stream().filter(user->"男".equal(user.getSex())).collect(Collectors.toList);

//用户列表按照性别进行分组
Map<String,List<User>> userMap=userList.stream().collect(Collectors.groupingBy(User::getSex));

//对流数据进行统计
Long count=userList.stream().count();

//选取前多少条数据
List<User> users=userList.stream().limit(2);

//跳过前多少条数据
List<User> users=userList.stream().skip(2);

//将两个相同类型的列表进行拼接
List<User> users=Stream.concat(list1.stream(),list2.stream()).collect(Collectors.toList());

//将对象中的某个字段值取出来重新生成一个列表
List<String> sexString=userList.stream().map(User::getSex).collect(Collectors.toList());

//将对象的两个字段组成一个map
Map<Long, String> collect1 = applyUsers.stream().collect(Collectors.toMap(ApplyUser::getApplyId, ApplyUser::getApplyDeptName));