new TIntLinkedList().insert(0, 42) throws NPE

Issue #58 new
Dmitry Groshev created an issue

The title says it all. This is not the case for new java.util.LinkedList<Integer>().add(0, 42) which does a very similar thing (inserts into given offset).

Even if this is an intended behaviour it's not documented.

Comments (3)

  1. Rob Eden

    Note that *LinkedList should be considered experimental. Use of *ArrayList is recommended for production. (Though I don't know what it's behavior would be in this case either.)

  2. Dmitry Groshev reporter

    Thanks for the comment. Unfortunately, I had a task where O(1) insertion is crucial, so I just added a condition on number of elements. However, I believe it would be better to do this in the library instead of NPE, especially given that Trove is the only widespread library with primitive linked lists, so there is no meaningful alternative.

  3. jimdavies
    • edited description
    • changed milestone to Future

    So you know, array lists in the general case will give you O(1) performance. Additionally, as they don't create a new object each time they create an element, they are much faster, and far more memory efficient. I've worked on a number of high performance Java projects where we have analysed in depth the performance characteristics of array lists and linked lists, and the performance hit of copying the array for extension is far less than the amortised cost of creating the container elements for linked lists. Also, when the lists get large, the cost of garbage collection becomes a severe hindrance for very large linked lists (>1m items), as they have to be walked from beginning to end by the garbage collector.

    However, this is very clearly a bug. Whilst I am currently prioritising core package bugs over experimental package bugs, I will get around to fixing this at some point.

  4. Log in to comment