Methods of the Collection interface

Figure below shows the methods of the Collection interface, grouped by their functionality: methods that modify Collection, methods that query it, and miscellaneous.

Because most of the interfaces implement the Collection interface, most of the imple- mentation classes include methods to add, remove, and query its elements and retrieve an Iterator to retrieve the collection elements. The implementation, though, varies across classes.

As you proceed and work with the implementation of classes in the collection framework, you’ll notice that almost all the classes mention that the Iterator returned by method iterator() is fail-fast. This implies that if you try to modify the structure of a collection by adding or removing elements from it, after the Iterator is created, the Iterator will throw the exception ConcurrentModificationException. But this doesn’t happen if you modify the collection by using the Iterator’s own add or remove methods. This important behavior prevents collections from returning arbitrary values during concurrent access.

As we move on to the next section to discuss more interfaces (namely, Set, List, and Deque), you’ll notice how each of them might suggest a different implementation of the methods from the Collection interface, to support the specific data structure that they represent.

Last updated