19

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)

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Victor
  • 16,609
  • 71
  • 229
  • 409

10 Answers10

29

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)

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 1
    They could have done better than stating 'proper sequence' :-) – The Nail Feb 27 '12 at 22:49
  • 6
    @TheNail - Well, the docs for `List` do say right at the start that a `List` is "An ordered collection (also known as a _sequence_)." – Ted Hopp Feb 27 '12 at 22:54
11

(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.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
7

It's in the specification of the List interface to preserve order.

It's the Set classes that don't preserve order.

trutheality
  • 23,114
  • 6
  • 54
  • 68
3

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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.

celopez3
  • 86
  • 1
1

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

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.

Andy
  • 8,870
  • 1
  • 31
  • 39
-1

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.

advait
  • 21
  • 3
-2

Even Sets will return the same result, if you don't modify them (adding or removing items to them).

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
  • 2
    In practice this is often true, but it isn't guaranteed (except for SortedSet!). In theory you could encounter a Set that reorganises its internal structure behind the scenes... – DNA Feb 27 '12 at 22:51
  • 2
    There's no guarantee that a Set will not reorganize itself internally at any time. There's no guarantee that set iterations will be in any particular order or that the order will not change from one iterator to the next. – Ted Hopp Feb 27 '12 at 22:52
  • See also this *related but not same* SO question and its answer: http://stackoverflow.com/questions/2704597/iteration-order-of-hashset – The Nail Feb 27 '12 at 23:00
  • Just iterate a HashSet once, and then iterate it again, the order of elements will be kept. I know there is no guarantee for this in the spec, but current implementation does this. – Amir Pashazadeh Feb 28 '12 at 04:56