listAll.stream().mapToLong(SubscribeOutBO::getUserId).collect(Collectors.toList());为什么报错
时间: 2024-03-12 14:49:04 浏览: 15
这段代码报错的原因是因为 `mapToLong(SubscribeOutBO::getUserId)` 返回的是一个 `LongStream` 类型的流,而不是一个 `List` 类型的集合,因此无法使用 `Collectors.toList()` 方法进行转换为List集合。
如果你想要将 `LongStream` 类型的流转换为 `List` 集合,可以使用 `boxed()` 方法将其转换为 `Stream<Long>` 类型的流,然后再使用 `Collectors.toList()` 方法进行转换,例如:
```
List<Long> userIdList = listAll.stream()
.mapToLong(SubscribeOutBO::getUserId)
.boxed()
.collect(Collectors.toList());
```
这样就可以将 `LongStream` 类型的流转换为 `List<Long>` 类型的集合了。
相关问题
if (e.getCityId() == null && e.getAreaId() == null) { List<Long> collect = subLists.stream().map(SubscribeOutBO::getUserId).collect(Collectors.toList()); listAll.addAll(collect);为什么两个参数都为null确走不进来
根据代码逻辑,如果`e.getCityId()`和`e.getAreaId()`都为null,那么`listAll`应该会添加`subLists`中所有元素的`userId`,即`subLists.stream().map(SubscribeOutBO::getUserId)`的结果。但是,这段代码并没有判断`subLists`是否为空,如果`subLists`为空,则`collect`是一个空列表,不会添加任何元素到`listAll`中。因此,你可以检查一下`subLists`是否为空,如果为空,可以添加一些日志输出来检查代码是否执行到了这里。
userList.stream().map(DopUserDTO::getUserId).collect(Collectors.toList()); 详细解释一下这段代码的意思
这段代码是 Java 8 中的流式操作,它的作用是将一个对象集合中的某个属性提取出来,然后将这些属性值放到一个新的集合中返回。具体来说,这段代码将 userList 集合中的每一个 DopUserDTO 对象的 userId 属性提取出来,然后装入一个新的集合中并返回。其中,map 方法将每一个对象映射为它的 userId 属性值,toList 方法将映射结果装入一个列表中返回。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![egg](https://img-home.csdnimg.cn/images/20210720083646.png)