I came across an error in the Java Collections API, in Collections.java.
Retrieves the code verbatim from the JDK source. As you know, the JavaDoc version tag reads “1.106, 04/21/06”. The method is located on 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 analyze the method, you will quickly find an error: T candidate = i.next (). Doh! Calling i.next () on an Iterator without checking hasNext ()? This is just an exception request.
Was it really necessary to notice something similar during the encoding? This means that using the API should check if the collection has at least two elements.
source
share