如何将构造函数作为函数传递给泛型方法,然后在 stream().map() 操作中使用该构造函数?
How do I pass a Constructor as a Function to a generic method and then use that Constructor in a stream().map() operation?
我有一个具体的 class MyObjectWrapper 和一个 Interface MyObject。
MyObjectWrapper implements MyObject
MyObjectWrapper.jsonObject 持有 MyObjectJson 的一个实例。
MyObjectWrapper 的构造函数签名是:
MyObjectWrapper(MyObjectJson myObjectJson, ServiceManager serviceManager)
我想为方法提供一个 PaginatedList 的实例,并包括对上述 MyObjectWrapper 构造函数的引用,以便它可以为 PaginatedList 中的每个项目引用,这样我的 stream() 的结果就是一个新的 PaginatedList 实例其中 MyObjectWrapper 是具体的 MyObject。
我将从调用 mapList 的任何地方引用 this.getServiceManager()。我假设我必须将 MyObjectWrapper 的构造函数作为函数传递,但不确定如何编写方法签名,也不确定如何在方法本身内将构造函数正确引用为映射函数。
我的代码如下所示,但我想使该方法通用,这样我就不必为每种类型都复制它。
PaginatedList<T> extends ArrayList<T>
private PaginatedList<MyObject> mapList(PaginatedList<MyObjectJson> paginatedList) {
return paginatedList.stream()
.map(jsonObject -> new MyObjectWrapper(jsonObject, this.getServiceManager()))
.collect(Collectors.toCollection(PaginatedList::new));
}
由于构造函数将 MyObjectJson
和 ServiceManager
作为输入,因此您不需要 Function
作为输入,而是 BiFunction<? super MyObjectJson, ? super ServiceManager, ? extends MyObject>
1:
private PaginatedList<MyObject> mapList(PaginatedList<MyObjectJson> paginatedList,
BiFunction<? super MyObjectJson, ? super ServiceManager, ? extends MyObject> myObjectProducer) {
return paginatedList.stream()
.map(jsonObject -> myObjectProducer.apply(jsonObject, this.getServiceManager()))
.collect(Collectors.toCollection(PaginatedList::new));
}
然后用 mapList(someList, MyObjectWrapper::new)
调用它。
1 为什么 super
/extends
,请阅读 What is PECS
我有一个具体的 class MyObjectWrapper 和一个 Interface MyObject。
MyObjectWrapper implements MyObject
MyObjectWrapper.jsonObject 持有 MyObjectJson 的一个实例。
MyObjectWrapper 的构造函数签名是:
MyObjectWrapper(MyObjectJson myObjectJson, ServiceManager serviceManager)
我想为方法提供一个 PaginatedList 的实例,并包括对上述 MyObjectWrapper 构造函数的引用,以便它可以为 PaginatedList 中的每个项目引用,这样我的 stream() 的结果就是一个新的 PaginatedList 实例其中 MyObjectWrapper 是具体的 MyObject。
我将从调用 mapList 的任何地方引用 this.getServiceManager()。我假设我必须将 MyObjectWrapper 的构造函数作为函数传递,但不确定如何编写方法签名,也不确定如何在方法本身内将构造函数正确引用为映射函数。
我的代码如下所示,但我想使该方法通用,这样我就不必为每种类型都复制它。
PaginatedList<T> extends ArrayList<T>
private PaginatedList<MyObject> mapList(PaginatedList<MyObjectJson> paginatedList) {
return paginatedList.stream()
.map(jsonObject -> new MyObjectWrapper(jsonObject, this.getServiceManager()))
.collect(Collectors.toCollection(PaginatedList::new));
}
由于构造函数将 MyObjectJson
和 ServiceManager
作为输入,因此您不需要 Function
作为输入,而是 BiFunction<? super MyObjectJson, ? super ServiceManager, ? extends MyObject>
1:
private PaginatedList<MyObject> mapList(PaginatedList<MyObjectJson> paginatedList,
BiFunction<? super MyObjectJson, ? super ServiceManager, ? extends MyObject> myObjectProducer) {
return paginatedList.stream()
.map(jsonObject -> myObjectProducer.apply(jsonObject, this.getServiceManager()))
.collect(Collectors.toCollection(PaginatedList::new));
}
然后用 mapList(someList, MyObjectWrapper::new)
调用它。
1 为什么 super
/extends
,请阅读 What is PECS