BananaBackend BananaBackend - 5 months ago 8
Java Question

Get last X items of a list in java

I know this question probably has been answered a frustrating amount of times, but I 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);

Thank you for your time.


Do use the method sub list

List<Post> myLastPosts = posts.subList(posts.size()-40, posts.size()-1);