gpt4 book ai didi

Java如何转换整个列表

转载 作者:行者123 更新时间:2023-12-01 18:04:53 25 4
gpt4 key购买 nike

如何在 Java 中转换完整列表而不进行迭代。我有这个功能:

private List<Object> getItems (List<Object> list, Pagination pagination) {

if (list.size() > pagination.getItemsXPage()) {

int init = (pagination.getCurrentPage()-1)* pagination.getItemsXPage();
int end = init + pagination.getItemsXPage();

if (end > list.size()) {
end = list.size();
}

return list.subList(init, end);

} else {

return list;
}

}

我想这样做,但不可能?

List<ApplicationSearchItem> applicationSearchItem = (List) getItems (list, searchCriteria.getCategoryProductsPagination()); 

最佳答案

在这种情况下,您将使用泛型方法声明:

By now, you will have learned to avoid the beginner's mistake of trying to use Collection< Object > as the type of the collection parameter. You may or may not have recognized that using Collection< ? > isn't going to work either. Recall that you cannot just shove objects into a collection of unknown type.

from oracle generic methods tutorial

返回类型由参数列表的类型定义:

private <T> List<T> getItems (List<T> list, Pagination pagination) {
...
}

关于Java如何转换整个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37348226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com