16

The List javadocs mentions that Lists are ordered. However, I cannot see anything specifying the nature of the ordering. Can we rely on lists e.g. ArrayList and LinkedList maintaining insertion order?

I am asking about the instance where we do not call set or sort.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Andy Cribbens
  • 1,370
  • 2
  • 11
  • 22
  • 8
    Yes. The add method says: "Appends the specified element **to the end** of this list". – JB Nizet Mar 06 '18 at 17:24
  • 1
    @JBNizet It does add it to the end, but is there anything that says the class is not allowed to move it at any point, or that the meaning of "end" is as we'd expect it to be? Of course it's true for ArrayList and LinkedList, but would it be true for any List not violating the interface contract? – Bernhard Barker Mar 06 '18 at 18:01
  • @Dukeling thank you for that. I updated the question accordingly. – Andy Cribbens Mar 06 '18 at 18:14
  • 1
    @AndyCribbens, if you are only calling a simple add() then yes, the order will be maintained correctly. But notice that there is add(index,value) which could also result in out of order. – Steve Mar 07 '18 at 02:25
  • 1
    Lists are ordered, i.e. their elements have *some* ordering. But this could be any order, not necessarily insertion-order. Nothing in the spec forbids to rearrange elements when a new element is appended, so the answer is **NO**: not all lists in Java maintain insertion order. To prove this, you could perfectly implement a `List` that sorts all its elements except for the last one, which should be placed at the end to not break the contract of the `add` operation. – fps Mar 07 '18 at 14:30
  • @fps https://beginnersbook.com/2014/07/difference-between-list-and-set-in-java/#:~:text=1)%20List%20is%20an%20ordered%20collection%20it,which%20they%20got%20inserted%20into%20the%20list. – user180708 Mar 28 '22 at 23:55
  • @user180708 If you are going to share a link, please share the official documentation instead of some blog... https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/List.html There it says nothing about insertion order – fps Mar 29 '22 at 15:31

1 Answers1

22

However, I cannot see anything specifying the nature of the ordering.

Funnily enough, it's mentioned in the second sentence of its documentation:

The user of this interface has precise control over where in the list each element is inserted.

Without calling List#set or List#sort, the only methods that will add elements to the List are List#add and List#addAll, both of which append them to the end (unless you call one of the overloaded methods and specify an index); you can also add elements to the List via its ListIterator.

If List did not maintain insertion order, it would have mentioned it in its documentation. If you're still unsure, feel free to look over the source code, which is available online.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • 4
    The essence of your answer is "If List did not maintain insertion order, it would have mentioned it in its documentation", although I don't agree that you can conclusively say this without any proof (the source code would only apply to the List classes in the standard API, not to what all List classes would be expected to adhere to). – Bernhard Barker Mar 06 '18 at 19:47
  • 1
    @Dukeling - he mentioned ArrayList and LinkedList specifically, so I think he is ok limiting this to List classes in the standard API. – Steve Mar 07 '18 at 02:26