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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With