brain storm
brain storm

Reputation: 31272

why does the Collections utility class does not have a Iterator method in Java?

The Collections utility class has static methods that operate on collection. For example, Collections.Sort(list) that sorts the list.

why not a method of type

static <T> Iterator<T> iterator() { }

In this way, I could get an iterator over my collection as

Iterator it = Colletions.iterator(list)

The Collections do have emptyIterator() and emptyListIterator(), but I dont understand what purpose do they serve?

Upvotes: 1

Views: 704

Answers (4)

Marko Topolnik
Marko Topolnik

Reputation: 200296

I'll answer only the "purpose of the empty iterator" part.

Consider this method:

boolean containsEvil(String x) {
  return x.contains("evil");
}

This method will fail for a null-argument, which is an edge case for it and it would need special handling to avoid the failure. However, if you take care in the rest of your program to never use null arguments and represent the concept of "no string" as an empty string, you don't need to handle the special case.

Occasionally you may also have an Iterator-based API:

boolean containsEvil(Iterator<String> xs) {
   while (xs.hasNext()) if (containsEvil(xs.next()) return true;
   return false;
}

Again, this will fail for a null-argument, so you will want to have an empty iterator factory around to stand for the "no iterator" case. In broader terms, you want your zero element in the iterator space, just as you want it in the string space.

Upvotes: 1

iajrz
iajrz

Reputation: 789

It doesn't need an iterator method because it's already defined in the Collection Interface. It needs be so, because for different Collection classes the iterators can't work the same (internally) even if they provide the same functionality (externally).

[EDIT. Thanks, Marko.] Empty iterators allow you to write cleaner code, eliminating the need to cover special edge cases, and there's only one way to provide an empty iterator. Thus, it's defined in the Collections class with its predefined behavior.

Upvotes: 2

rgettman
rgettman

Reputation: 178343

There's no need for a Collections.iterator(Collection) method, because the Collection interface has an iterator() method that returns an Iterator already. It only makes sense when you have a Collection already, and the Iterator is coupled with the Collection anyway. What would Collections.iterator(Collection c) do? It would probably just call return c.iterator();.

I suppose that Collections.emptyIterator() and Collections.emptyListIterator() save the caller the processing to create an empty Collection / List just to call its iterator() / listIterator() method.

The GRASP pattern "Information Expert" would indicate that the design of an Iterator would be with the class it iterates, because the class itself has the information needed to create an Iterator.

Upvotes: 2

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236150

But we have Collection.iterator(), that's exactly what you're looking for.

The Collections class contains a bunch of utility methods that are generic for all kinds of collections, whereas all the collections that implement the Collection interface contain specific implementations of a given functionality.

Because each Collection already has its own iterator, it doesn't make sense to have it as an utility method in Collections, besides it would be trivial to implement: just return the result of calling the iterator() method on the Collection received as parameter.

Upvotes: 1

Related Questions