ABSTRACT

While most methods in the Collection interface depend upon details of the internal representation, some methods can be implemented here and shared by all data structures that implement a collection. Some of these methods involve maintaining and accessing the number of elements in the collection (e.g., size, isEmpty), and others are implemented in terms of abstract methods like add, making use of the template method design pattern, as we do often throughout this book. In some cases, the implementations provided here are “brute-force,” implementations but they can be overridden when a more efficient algorithm exists for a specific data structure. Also, the methodology and associated instance variable version, for triggering a ConcurrentModificationException when appropriate, is included here to be inherited by all collection data structures.