【发布时间】:2021-05-23 17:28:24
【问题描述】:
我正在尝试将ComplexItem 的列表转换为它们对应的 ID 列表Long。但是即使在使用(Collection<ComplexItem>) 对getCollection() 调用进行类型转换后也不会出现上述错误
Set<Long> ids = Collections2.transform(
getComplexItems(), new Function<ComplexItem, Long>() {
@Override public Long apply(final ComplexItem item) {
return item.id;
}
}));
public List<ComplexItem> getComplexItems() {
..............
}
【问题讨论】:
-
为什么不干脆做
getComplexItems().stream().map(item -> item.id).collect(Collectors::toSet)? -
@QBrute 应该可以工作,但想知道上面的代码有什么问题
标签: java collections transform