I'm not certain if the enumerator is reset if an enumeration is not completed, so if that's a worry, then who knows at what point the enumeration would start? Enumerating an array would always start at the beginning.
These two sentences make me think that you have deep misunderstandings about how the enumerable pattern works. Can you explain why you think that an abandoned enumerator has anything whatsoever to do with a later enumeration?
An enumerable is a device which produces enumerators. If an enumerator is abandoned, that does not in any way affect the enumerable. If I sell books, and Bob buys a book and only reads it halfway, that doesn't mean that when I sell a different copy of the book to Alice, she has to start reading where Bob left off.
I'm always stumped as to what to extend: IEnumerable<T>
or T[]
. My rationale was clarity of purpose: An array has a notion of ordering, whereas an IEnumerable is implemented by many generic collections, not all of which have a notion of order:
Does your extension method need to (1) access the collection out of order? (2) write to the collection?
If so, then extend IList<T>
If not, extend IEnumerable<T>
.
What about: (3) pass the collection on to a method that expects an array?
Then extend array.
Your question is basically "I don't know whether to extend Animal or Giraffe". If your extension method is not specific to Giraffes, extend Animal. If your extension method is not specific to arrays, extend all lists. If it is not specific to lists, extend all sequences. If it is not specific to sequences, odds are good that an extension method is the wrong mechanism for you; I recommend against extending all objects.
Although it is correct from a narrowly technical point of view to say that Set
implements Iterable
, but SortedMap
does not, it is not a very useful question to ask. A more useful question to ask is whether both classes offer the Iterable
interface, and they certainly both do.
As you know, there are three ways to offer an interface:
- By explicitly implementing it using the
implements
keyword.
- By implementing an interface which extends the interface in question.
- By exposing a function which returns the interface in question.
Set
is declared as follows: public interface Set<E> extends Collection<E> {...
so, as you can see, Set
does not directly implement Iterable
, it only implements it indirectly, by virtue of implementing Collection
, which extends Iterable
.
Now, SortedMap
would also be implementing Iterable
indirectly, just as Set
does, if it was implementing Collection
, but it does not even do that. Instead, it exposes an entrySet()
function, which returns a Collection
, which extends Iterable
.
So, you can have your much desired Iterable
in both cases.
If you would like to insist on the narrowly technical question of why Set
(indirectly) implements Iterable
, but SortedMap
offers it via a function instead, the answer is that the designers of the java language runtime made the decision a long time ago to refrain from making extensive use of interfaces extending other interfaces. That's why Map<K,V>
does not extend Collection<Map.Entry<K,V>>
, offering an entrySet()
method instead.
They could have done the opposite, and as a matter of fact the designers of the C# language runtime did choose to go the other way, so IDictionary<K,V>
extends ICollection<KeyValuePair<K,V>>
.
I have experimented with both ways, and I still have not arrived at a conclusive decision as to which one is better. They simply appear to be two different ways of accomplishing the same thing.
The implementation of Iterator
does not imply a stable ordering, as you seem to understand (and therefore it is not clear to me where your question is in that respect.) You may iterate over an unsorted map, in which case nobody can tell what the order of the items will be, and you may insert an element to the map and re-iterate, in which case the order of the items may be completely different from the previous time. (Not just different by one item: all items may appear to be re-arranged.)
There is no need for an OrderedIterable
interface because its signature would be identical to Iterable
. (And don't get me started on the choice of the java people to introduce a Set
besides Collection
.) You can just invoke OrderedMap.entrySet()
, which the documentation promises will return an ordered set, which means that the Iterator
of that set will always yield items in order.
Best Answer
Here is an answer from stackoverflow that explains it pretty well:
https://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster
Basically, the ArrayList is contiguous where the LinkedList is not. Incrementing to the next location in memory with the ArrayList is considered faster than jumping to the next location via a reference in LinkedList. Also, maintenance of the LinkedList would incur overhead to maintain two sets of references for a doubly linked list.