24.5. Example - Flashing Text

 
[Page 715 ( continued )]

22.2. The Collection Interface and the AbstractCollection Class

The Collection interface is the root interface for manipulating a collection of objects. Its public methods are listed in Figure 22.3. The AbstractCollection class is a convenience class that provides partial implementation for the Collection interface. It implements all the methods in Collection except the size and iterator methods. These are implemented in appropriate subclasses.

Figure 22.3. The Collection interface contains the methods for manipulating the elements in a collection, and each collection object contains an iterator for traversing elements in the collection.

The Collection interface provides the basic operations for adding and removing elements in a collection. The add method adds an element to the collection. The addAll method adds all the elements in the specified collection to this collection. The remove method removes an element from the collection. The removeAll method removes the elements from this collection that are present in the specified collection. The retainAll method retains the elements in this collection that are also present in the specified collection. All these methods return boolean . The return value is true if the collection is changed as a result of the method execution. The clear() method simply removes all the elements from the collection.


[Page 716]

Note

The methods addAll , removeAll , and retainAll are similar to the set union, difference, and intersection operations.


The Collection interface provides various query operations . The size method returns the number of elements in the collection. The contains method checks whether the collection contains the specified element. The containsAll method checks whether the collection contains all the elements in the specified collection. The isEmpty method returns true if the collection is empty.

The Collection interface provides the toArray() method that returns an array representation for the collection.

A collection may be a set or a list. The Iterator interface provides a uniform way for traversing elements in various types of collections. The iterator method in the Collection interface returns an instance of the Iterator interface, as shown in Figure 22.3, which provides sequential access to the elements in the collection using the next () method. You can also use the hasNext() method to check whether there are more elements in the iterator, and the remove() method to remove the last element returned by the iterator.

Note

Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException , a subclass of RuntimeException . This is a good design that you can use in your project. If a method has no meaning in the subclass, you can implement it as follows :

   public void   someMethod() {   throw new   UnsupportedOperationException(   "Method not supported"   ); } 


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net