I. Overview
The Java Collection Framework defines a series of classes that are containers for storing data. Compared to arrays, StringBuffer (StringBuilder), it is characterized by:
1. Used to store objects.
2. The set length is variable.
3. The base data type cannot be stored.
Compare the characteristics of three types of containers:
The array must hold the same element.
StringBuffer must be converted to a string to be used if it is almost impossible to come up with a single element.
Data has a lot of objects to use, objects have many, and use collections to save.
Collection container because the internal data structure has a variety of specific containers, the continuous upward extraction of the formation of a set of framework.
The top layer of the frame is the collection interface.
Second, collection interface
Both the set interface and the list interface implement the collection interface, so it is obvious that the collection interface holds the common content of the set interface and the list interface.
Methods in the collection interface:
1. Add.
boolean |
add(E e) Make sure that this collection contains the specified element (optional action). |
boolean |
addAll(Collection<? extends E> c) Adds all the elements in the specified collection to this collection (optional action). |
Where the parameter e can be temporarily understood as an object (actually used before JDK1.4 is always an object).
2. Delete
boolean |
Remove (object o) removes a single instance of the specified element from this collection, if one exists (optional action). |
; Boolean |
removeall (collection<?> c) removes all the elements in this collection that are also contained in the specified collection (optional action). |
void |
clear() Removes all the elements in this collection (optional action). |
3. Judging
boolean |
contains (object o) returns true if this collection contains the specified element. |
; Boolean |
containsall (collection<?> c) returns true if this collection contains all the elements in the specified collection. |
boolean |
isEmpty() Returns Trueif this collection contains no elements. |
4. Access.
int |
size() Returns the number of elements in this collection. |
Iterator<E> |
iterator() Returns an iterator that iterates over the elements of this collection. |
5. Other
boolean |
retainAll(Collection<?> c) Leave only those elements in this collection that are also contained in the specified collection (optional action). |
Object[] |
toArray() Returns an array that contains all the elements in this collection. |
|
toArray(T[] a) Returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array. |
"List and set of the Java Collection framework"