Reputation: 17097
I have a java ArrayList
to which I add 5 objects.
If I iterate over the list and print them out, then iterate over the list and print them out again.
Will the retrieval order in these 2 cases be the same? (I know it may be different from the insertion order)
Upvotes: 19
Views: 35736
Reputation: 21
Yes the retrieval order is guaranteed to be the same as long as list is not mutated and you use the same iterator, but having to need to rely on retrieval order indicated something fishy with the design. It is generally not a good idea to base business logic upon certain retrieval order.
Upvotes: -1
Reputation: 236004
Yes, an ArrayList
guarantees iteration order over its elements - that is, they will come out in the same order you inserted them, provided that you don't make any insertions while iterating over the ArrayList
.
Upvotes: 2
Reputation: 120516
(I know it may be different from the insertion order)
No it won't. The contract of List
requires that the add
order is the same as the iteration order, since add
inserts at the end, and iterator
produces an iterator that iterates from start to end in order.
Set
doesn't require this, so you may be confusing the contract of Set
and List
regarding iteration order.
From the Javadoc:
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
Upvotes: 11
Reputation: 9048
The iteration order will be the same everytime you iterate over the same unmodified list.
Also, assuming you add the elements using the add()
method, the iteration order will be the same as the insertion order since this method appends elements to the end of the list.
Upvotes: 1
Reputation: 234807
When you add an element to an ArrayList using add(E e)
, the element is appended to the end of the list. Consequently, if all you do is call the single-argument add
method a number of times and then iterate, the iteration will be in exactly the same order as the calls to add
.
Upvotes: 1
Reputation: 86
Retrieval does not vary unless you change the iterator you are using. As long as you are using the same method for retrieval and have not changed the list itself then the items will be returned in the same order.
Upvotes: 1
Reputation: 23465
It's in the specification of the List
interface to preserve order.
It's the Set
classes that don't preserve order.
Upvotes: 7
Reputation: 7302
Even Sets will return the same result, if you don't modify them (adding or removing items to them).
Upvotes: -2
Reputation: 272517
Yes, assuming you haven't modified the list in-between. From http://docs.oracle.com/javase/6/docs/api/java/util/List.html:
iterator
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
A bit vague, perhaps, but in other portions of that page, this term is defined:
proper sequence (from first to last element)
Upvotes: 29
Reputation: 198103
If you're not mutating the list, then the iteration order will stay the same. Lists have a contractually specified ordering, and the iterator
specification guarantees that it iterates over elements in that order.
Upvotes: 3