关于list.stream().map(User::getUserId).collect(Collectors.toList())的写法

今天看同事的代码,见到下面这一段,没看懂,去网上百度了一下,才明白是什么意思

List<User> UserList = userMapper.selectAllUser();
List<String> userIdList = UserList.stream().map(User::getUserId).collect(Collectors.toList());

等价于

List<User> UserList = userMapper.selectAllUser();
List<String> userIdList = new ArrayList<>();
for(User user : UserList){
     
	userIdList.add(user.getUserId());
}

你可能感兴趣的:(学习)