Java 如何将一页对象转换为 spring 数据中的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39135000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:54:00  来源:igfitidea点击:

How to convert a page of objects to list in spring data

javaspring-data

提问by DanielD

I have a page of objects like this:

我有一个像这样的对象页面:

Page<Video> videos = videoRepository.findAllVideos(new PageRequest(1, 50));

How can I convert that to a list of Video objs without iterating over my page?

如何在不遍历我的页面的情况下将其转换为视频对象列表?

采纳答案by sathyendran a

Page<Video> videos = videoRepository.findAllVideos(new PageRequest(1, 50));
List<Video> videosList = videos.getContent();

You can use the above code to get the list of videos from page

您可以使用上面的代码从页面获取视频列表

回答by Shessuky

Try this solution (and avoid all exception risks) :

试试这个解决方案(并避免所有异常风险):

Page<Video> videos = videoRepository.findAllVideos(new PageRequest(1, 50));
List<Video> videoList = new ArrayList<Video>();
if(videos != null && videos.hasContent()) {
    videoList = videos.getContent();
}