The core Collection interface

The Collection interface implements the Iterable interface, which defines method iterator(), enabling all the concrete implementations to access an Iterator to iterate over all the collection objects. So for all the implementing classes, you’d be able to access an Iterator to iterate over its elements.

When you work with the concrete implementations of the collections framework, you’ll notice that almost all the classes provide two constructors: a void (no-argument) constructor and another that accepts a single argument of type Collection. The former creates an empty collection, and the latter creates a new collection with the same elements as its argument, dropping any elements that don’t fit the new collection being created. As an interface, Collection can’t enforce this requirement because it can’t define a constructor. But the classes that implement this interface implement it.

The methods of the Collection interface aren’t marked synchronized. The synchronized methods result in a performance drop, even in single-threaded code, and so the creators of the collections framework opted out for them. If you’ve worked with or read about the Hashtable or Vector classes, which were introduced before the Java collections framework, you’d notice that the methods of these data structures are synchronized. With the existing collections framework, you can get the same functionality (without synchronization) by using the collection classes HashMap and ArrayList.

Last updated