我有如下一个方法,问:如何将该方法改成泛型方法,接收任意类型的入参,并返回对应类型的出参,就是将方法入参中指定的 IlsGoods 类替换成任意类型
/**
* 拆分集合
*
* @param ilsGoodsList 目标集合
* @param sonSize 子集合大小
* @return
*/
private List> splitSet(List ilsGoodsList, int sonSize) {
//分割的份数
final int copies = (ilsGoodsList.size() + sonSize - 1) / sonSize;
//映射分割
List> sonListTwo = IntStream.range(0, copies)
.boxed()
.parallel()
.map(i -> {
int fromIndex = i * sonSize;
int toIndex = sonSize;
if (i + 1 == ilsGoodsList.size()) {
toIndex = ilsGoodsList.size() - fromIndex;
}
return ilsGoodsList.stream().skip(fromIndex).limit(toIndex).collect(Collectors.toList());
}).collect(Collectors.toList());
return sonListTwo;
}
你如果确定要处理的逻辑只和list相关,和里面存什么不相关(不调用内部存储的对象的方法)
那你可以写List<List<T>>,或者写List<ArrayList>
这样就可以了
private <T> List<List<T>> splitSet(List<T> ilsGoodsList, int sonSize) {
//分割的份数
final int copies = (ilsGoodsList.size() + sonSize - 1) / sonSize;
//映射分割
List<List<T>> sonListTwo = IntStream.range(0, copies)
.boxed()
.parallel()
.map(i -> {
int fromIndex = i * sonSize;
int toIndex = sonSize;
if (i + 1 == ilsGoodsList.size()) {
toIndex = ilsGoodsList.size() - fromIndex;
}
return ilsGoodsList.stream().skip(fromIndex).limit(toIndex).collect(Collectors.toList());
}).collect(Collectors.toList());
return sonListTwo;
}
如有帮助,欢迎采纳哈!