Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last X items of a list in java

I can only seem to find answers about last/first element in a list or that you can get a specific item etc.

Lets say I have a list of 100 elements, and I want to return the last 40 elements. How do I do that? I tried doing this but it gave me one element..

Post last40posts = posts.get(posts.size() -40);
like image 525
BananaBackend Avatar asked May 13 '16 21:05

BananaBackend


1 Answers

Do use the method sub list

List<Post> myLastPosts = posts.subList(posts.size()-40, posts.size());
like image 106
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 10 '22 20:10

ΦXocę 웃 Пepeúpa ツ