Name

Collection<E>

Synopsis

This interface represents a group, or collection, of objects. In Java 5.0 this is a generic interface and the type variable E represents the type of the objects in the collection. The objects may or may not be ordered, and the collection may or may not contain duplicate objects. Collection is not often implemented directly. Instead, most collection classes implement one of the more specific subinterfaces: Set, an unordered collection that does not allow duplicates, or List, an ordered collection that does allow duplicates.

The Collection type provides a general way to refer to any set, list, or other collection of objects; it defines generic methods that work with any collection. contains( ) and containsAll( ) test whether the Collection contains a specified object or all the objects in a given collection. isEmpty( ) returns true if the Collection has no elements, or false otherwise. size( ) returns the number of elements in the Collection. iterator( ) returns an Iterator object that allows you to iterate through the objects in the collection. toArray( ) returns the objects in the Collection in a new array of type Object. Another version of toArray( ) takes an array as an argument and stores all elements of the Collection (which must all be compatible with the array) into that array. If the array is not big enough, the method allocates a new, larger array of the same type. If the array is too big, the method stores null into the first empty element ...

Get Java in a Nutshell, 5th 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.