Preface :
A collection is an object that holds other objects. More precisely, it is a storing mechanism with operations that allow for adding and removing elements and for accessing and perhaps updating their values. As we proceed in this topic, we will encounter of variety of different collections. For now, we want to start at the very beginning and describe a simple collection that serves as a prototype data structure. In the process, we will define generic Collection interface, which defines a core set of operations that characterize the behavior of most collection classes. In our integrated development of data structures, the different collection types share this common set of operations.
A bag, say a bag of golf balls, is a model for the most general kind of collection. You toss things into the bag, pull things out of the bag, and reach in to search for an item in the bag. The Collection interface provides a template for this basic kind of collection. The interface defines operations add() and remove() that insert and delete elements from the collection. The operations return true or false, indicating whether any change actually occurred. The design of the operations reinforces the fact that the interface is a very general description of a collection. A collection class that implements the interface may or may not allow duplicates and so an add() operation may not actually insert a new element. The method clear() removes all elements from the collection and allows it to be reused as a storage structure. At any time, we need to know how many elements are in the collection. The method size() and isEmpty() specify the number of elements in the collection and indicate when the collection has no elements.
More about collection :
A collection is a dynamic-storage structure. It constantly changes as elements enter and leave. We need some way to find what is in the collection. The method contains() indicates whether a specific element is in the collection(true) or is not in the collection(false). At any time, a program must have a way to access the element in a collection. This function is provided by an iterator object of type Iterator, which has methods that allow it to sequentially scan each element and return the value. Responsibility to create an iterator and manage its behaviors belongs to the collection. The Collection interface defines the method iterator(), which returns an Iterator object positioned as a specific element. A collection class that implements the interface defines its own version ofIterator. The order in which elements are scanned depends on how the collection stores and accesses its elements. The notion of an iterator is fundamental to an understanding of modern data structures. We will examine the concept in Chapter 12.
We would usually assume that a collection stores objects. In fact, it only holds references to objects. An actual object is created independently and assigned to a reference variable. Adding an object to a collection involves assigning the reference to a new variable in the collection. Removing an element simply deletes the reference variable in the collection without destroying the object. The fact that a collection stores references is critical to your understanding of data structures. The Collection interface defines the method toArray() that copies the elements in the collection to an Object array. The resulting array is a new set of elements (references) that can be used to access the objects associated with the collection.
The array allows access with an index in the range 0 to size()-1. The array and the collection are separate entities. Sorting elements in the array doesn't change the ordering of elements in the collection.
The following is an API for the generic Collection interface. Note that the add() method requires a generic argument so that the collection contains only elements of the specified type. The methods contains() and remove() have an Object argument. Typically, a collection class implements these methods using equals(), which returns false when the comparison involves mixed types. As a result, the methods return false when a program attempts to identify or remove an element that is not of the generic type :
Supplement :
* JDK : Interface Collection
A collection is an object that holds other objects. More precisely, it is a storing mechanism with operations that allow for adding and removing elements and for accessing and perhaps updating their values. As we proceed in this topic, we will encounter of variety of different collections. For now, we want to start at the very beginning and describe a simple collection that serves as a prototype data structure. In the process, we will define generic Collection interface, which defines a core set of operations that characterize the behavior of most collection classes. In our integrated development of data structures, the different collection types share this common set of operations.
A bag, say a bag of golf balls, is a model for the most general kind of collection. You toss things into the bag, pull things out of the bag, and reach in to search for an item in the bag. The Collection interface provides a template for this basic kind of collection. The interface defines operations add() and remove() that insert and delete elements from the collection. The operations return true or false, indicating whether any change actually occurred. The design of the operations reinforces the fact that the interface is a very general description of a collection. A collection class that implements the interface may or may not allow duplicates and so an add() operation may not actually insert a new element. The method clear() removes all elements from the collection and allows it to be reused as a storage structure. At any time, we need to know how many elements are in the collection. The method size() and isEmpty() specify the number of elements in the collection and indicate when the collection has no elements.
More about collection :
A collection is a dynamic-storage structure. It constantly changes as elements enter and leave. We need some way to find what is in the collection. The method contains() indicates whether a specific element is in the collection(true) or is not in the collection(false). At any time, a program must have a way to access the element in a collection. This function is provided by an iterator object of type Iterator, which has methods that allow it to sequentially scan each element and return the value. Responsibility to create an iterator and manage its behaviors belongs to the collection. The Collection interface defines the method iterator(), which returns an Iterator object positioned as a specific element. A collection class that implements the interface defines its own version ofIterator. The order in which elements are scanned depends on how the collection stores and accesses its elements. The notion of an iterator is fundamental to an understanding of modern data structures. We will examine the concept in Chapter 12.
We would usually assume that a collection stores objects. In fact, it only holds references to objects. An actual object is created independently and assigned to a reference variable. Adding an object to a collection involves assigning the reference to a new variable in the collection. Removing an element simply deletes the reference variable in the collection without destroying the object. The fact that a collection stores references is critical to your understanding of data structures. The Collection interface defines the method toArray() that copies the elements in the collection to an Object array. The resulting array is a new set of elements (references) that can be used to access the objects associated with the collection.
The array allows access with an index in the range 0 to size()-1. The array and the collection are separate entities. Sorting elements in the array doesn't change the ordering of elements in the collection.
The following is an API for the generic Collection interface. Note that the add() method requires a generic argument so that the collection contains only elements of the specified type. The methods contains() and remove() have an Object argument. Typically, a collection class implements these methods using equals(), which returns false when the comparison involves mixed types. As a result, the methods return false when a program attempts to identify or remove an element that is not of the generic type :
Supplement :
* JDK : Interface Collection
This message was edited 2 times. Last update was at 24/09/2010 23:55:57
沒有留言:
張貼留言