There is List.addAll(position, collection)
but I can't find a corresponding List.<remove>(position, <range>)
, whatever the wording of and the way is specified would be. In ArrayList
, there is removeRange(position, positionAfter)
, but not in List
.
Removing an interval from a list can be O(1) in many list types, removing elements one by one would be O(n) and miss that ability. Converting a general List
to a type that supports range removal would also be O(n) and miss the chance.
Is there any way to remove a range of values from a general List
in a way that the List
implementations can optimize for this?
The List
method subList
returns a view of a subrange of the list, backed by the same data: Alterations made to the sublist are reflected in the original.
So you can use subList
and clear
to remove a block of elements from a mutable list:
>>> List<Integer> values = new ArrayList<>(List.of(10,20,30,40,50,60));
values ==> [10, 20, 30, 40, 50, 60]
>>> values.subList(3,5).clear()
>>> values
values ==> [10, 20, 30, 60]