Humphrey Bogart
Humphrey Bogart

Reputation: 7613

Java Collections API Bug?

I've stumbled upon a bug in the Java Collections API, in Collections.java.

Here’s the code verbatim from the JDK’s source. Just so you know, the JavaDoc version tag reads "1.106, 04/21/06". The method is located in line 638.

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
    Iterator<? extends T> i = coll.iterator();
    T candidate = i.next();

    while (i.hasNext()) {
        T next = i.next();
        if (next.compareTo(candidate) > 0)
            candidate = next;
    }
    return candidate;
}

If you take a second to analyse the method, you’ll quickly spot the bug: T candidate = i.next(). D’oh! Calling i.next() on an Iterator without checking hasNext() first? That’s just asking for an exception.

Surely something like that should've been spotted during coding? It means use of the API must check if the collection has at least two elements.

Upvotes: 3

Views: 1475

Answers (3)

topchef
topchef

Reputation: 19793

According to API doc Collection.max method throws NoSuchElementException if collection is empty.

This is exactly what you observed.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500785

No - it means that it's invalid to try to find the maximal element of an empty collection. This is specified in the API docs:

Throws:
    NoSuchElementException - if the collection is empty.

That's what Iterator.next() is documented to throw if there's no next element, so it's doing exactly what it's meant to.

Note that after the first call to next(), there's a call to hasNext() to check whether there's more than one element.

Upvotes: 21

Steve Reed
Steve Reed

Reputation: 2541

Hard to call it a bug since the exception is documented here

Throws:

  • ClassCastException if the collection contains elements that are not mutually comparable (for example, strings and integers).
  • NoSuchElementException if the collection is empty.

Upvotes: 7

Related Questions