How can I just get the last record in the java.util.List using subList() or any other method?

mannedear :

Say I have a list of elements whose size is 100. Now I only want the 100th record in the list and the rest of all records from 1-99 should be removed from the list.

I have tried the below piece of code but no change in list size as I see it.
//Output list.size() returns 100

list.subList(list.size()-1, list.size()); 

//Output list.size() returns 100 after subList() called...
How can I get just the last record in the java.util.List using subList() or using any other methods available in Java?

Dogukan Zengin :

ArrayList.subList method returns a sublist of your list without modifying your existing list.

So you need to do;

list = list.subList(list.size()-1, list.size()); 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459808&siteId=1