java-stream-List of Long to list in one line
java-stream-List of Long to list of int in one line
所以我知道我可以用两行 .stream
来做到这一点,但不确定我是否可以只用一行来做到这一点。
这是我拥有的:
List<Long> abcIds= abcController.findByUserIds(userIds)
.stream()
.map(Abc::getAbcId)
.collect(Collectors.toList());
但我希望 abcIds 是一个整数列表,因为稍后我将在其他函数中使用它。我知道我可以像这样写另一行来将 Long List 转换为 Integer List :
List<Integer> abcIntIds= abcIds.stream()
.map(Long::intValue)
.collect(Collectors.toList());
但是有没有办法写的更优雅一些呢?
为什么不 map
两次?
abcController.findByUserIds(userIds)
.stream()
.map(Abc::getAbcId)
.map(Long::intValue)
.collect(Collectors.toList());
所以我知道我可以用两行 .stream
来做到这一点,但不确定我是否可以只用一行来做到这一点。
这是我拥有的:
List<Long> abcIds= abcController.findByUserIds(userIds)
.stream()
.map(Abc::getAbcId)
.collect(Collectors.toList());
但我希望 abcIds 是一个整数列表,因为稍后我将在其他函数中使用它。我知道我可以像这样写另一行来将 Long List 转换为 Integer List :
List<Integer> abcIntIds= abcIds.stream()
.map(Long::intValue)
.collect(Collectors.toList());
但是有没有办法写的更优雅一些呢?
为什么不 map
两次?
abcController.findByUserIds(userIds)
.stream()
.map(Abc::getAbcId)
.map(Long::intValue)
.collect(Collectors.toList());