Remove first 'n' elements from list without iterating

create subList()

Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

Check implementation of this method and make some tests to determine performance


Jigar Joshi's answer is already contains the solution you need. I wanted to add some other stuff. Calling clear() on the sublist will handle your work, I guess. But it might be using iteration in the background, I'm not sure. Example script for your use:

ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> subList = (ArrayList<Integer>) list.subList(0, 9);
subList.clear();

Tags:

Java

List