list.subList returns a new List supported by the original List .
You need to store the returned list in a variable in order to use it:
List<String> subList = list.subList(list.size()-1, list.size());
subList.size() will be equal to 1. List will remain unchanged.
If you want to remove everything except the last item from the original List , you can write:
list.subList(0, list.size()-1).clear();
Now the original List will contain only one element.
source share