Chapter 9. The Collection Interface
The interface Collection<E>
defines the core functionality that we expect of any collection other than a map. It provides methods in four groups: adding elements, removing them, querying the collection’s contents, and making its elements available for further processing.
Adding Elements
boolean add(E e) // adds the element e boolean addAll(Collection<? extends E> c) // adds the contents of c
The contracts for these methods specify that after their execution, the collection must contain the element or elements supplied in the argument. So if a method call fails because, for example, it has attempted to add null
to a null
-hostile collection, it must throw an exception. But calls can succeed without changing the contents of the collection if, for example, they attempt to add an element to a set that already contains it. In this case, the result returned will be false, indicating that the collection ...
Get Java Generics and Collections, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.