Truncate a LinkedList

Daniel Fuchs daniel.fuchs at oracle.com
Tue Feb 19 10:38:14 UTC 2013


On 2/19/13 11:27 AM, Weijun Wang wrote:
> Hi All
>
> I'm using LinkedList to maintain a history and the elements are ordered
> by their timestamps. Every now and then I would "expunge" the list, that
> is to say, iterating through the list and when an element is old enough
> all elements after (and including) it will be removed. Currently I'm
> removing them one by one.
>
> Is there a way to truncate the list using a single method?
>
> Thanks
> Max

Hi Max,

You could try to use AbstractList.subList(...)

<http://docs.oracle.com/javase/6/docs/api/java/util/AbstractList.html#subList%28int,%20int%29>

Quoting from the doc:

> For example, the following idiom removes a range of elements from a list:
>
>       list.subList(from, to).clear();
>

-- daniel





More information about the core-libs-dev mailing list