Java Reference
In-Depth Information
Collection<T> interface). This turns out to be a very powerful tool. Let us explore the
possibilities. So far, we have seen one class that implements the Collection<T> interface,
namely the class ArrayList<T> . In addition to the methods given in Chapter 14 for the
ArrayList<T> class, the ArrayList<T> class also implements all the methods given
in Display 16.2. There are a number of different predefined classes that implement the
Collection<T> interface, and you can define your own classes to do this. If you write a
method to manipulate a parameter of type Collection<T> , it will work for all of these
classes. Also, the methods in the Collection<T> interface ensure that you can intermix
the use of different collection classes. For example, consider the method
public boolean containsAll(Collection<?> collectionOfTargets)
You can use this with two ArrayList<T> objects (the calling object and the argument)
to see if one contains all the elements of the other. The two ArrayList<T> objects
do not even have to have the same base type. Moreover, you can also use it with
an ArrayList<T> object and an object of any other class that implements the
Collection<T> interface to compare the elements in these two different kinds of
Collection<T> objects.
Display 16.2
Method Headings in the Collection<T> Interface (part 1 of 3)
The Collection<T> interface is in the java.util package.
CONSTRUCTORS
Although not officially required by the interface, any class that implements the Collection<T>
interface should have at least two constructors: a no-argument constructor that creates an empty
Collection<T> object, and a constructor with one parameter of type Collection<? extends
T> that creates a Collection<T> object with the same elements as the constructor argument.
The interface does not specify whether the copy produced by the one-argument constructor is a
shallow copy or a deep copy of its argument.
METHODS
boolean isEmpty()
Returns true if the calling object is empty; otherwise returns false .
public boolean contains(Object target)
Returns true if the calling object contains at least one instance of target . Uses target.
equals to determine if target is in the calling object.
public boolean containsAll(Collection<?> collectionOfTargets)
Returns true if the calling object contains all of the elements in collectionOfTargets . For
an element in collectionOfTargets , this method uses element.equals to determine if
element is in the calling object.
(continued)
 
Search WWH ::




Custom Search