java.util.LinkedList clear() improvement

Guy Korland gkorland at gmail.com
Thu Aug 27 12:31:13 UTC 2009


Hi,


It seems like linkedList.clear() can be easily fixed to O(1) instead of O(n).

Meaning:


      308     public void clear() {

      309         Entry<E> e = header.next;

      310         while (e != header) {

      311             Entry<E> next = e.next;

      312             e.next = e.previous = null;

      313             e.element = null;

      314             e = next;

      315         }

      316         header.next = header.previous = header;

      317         size = 0;

      318         modCount++;

      319     }


A better implementations would be:


      308     public void clear() {

      309         header.next = header.previous = header;

      310         size = 0;

      311         modCount++;

      312     }


Guy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20090827/c3e14567/attachment.html>


More information about the core-libs-dev mailing list