有没有以下内容的常见简写形式?欢迎使用Guava等外部依赖。
如果我必须实现它,我的做法可能是这样的:
myList.stream().map(Foo::bar).collect(Collectors.toList());
如果我必须实现它,我的做法可能是这样的:
static <T, U> List<U> mapApply(List<T> list, Function<T, U> function) {
return list.stream().map(function).collect(Collectors.toList());
}
有没有一个适用于任何可迭代对象的函数?如果没有,我该如何编写?我开始像这样思考:
static <T, U, V extends Iterable> V<U> mapApply(V<T> iterable, Function<T, U> function) {
return iterable.stream().map(function).collect(???);
}
V<U>
是无效的)。我也很想拥有这个功能! - FelixIterable
中,你要收集的对象必须支持像add
等方法。你可以接受任何Collection
,然后使用Collectors.toCollection
或者直接接受一个Collector
,这样就可以完全自定义了。 - ZabuzardEnumSet<Thread.State>
,而映射函数是Thread.State::toString
,那该怎么办? - Holger