Java.util.collection<e> Source Code Analysis
---------------------------------------------------------------------------------
java.util.collection<e> is an interface that is defined as follows :
1 public interface collection<e> extends iterable<e> { 2 // Query Operations 3 // modification Operations 4 Bulk Operations 5 // Comparison and hashing 6 }
(1) It can be seen from the definition of the collection interface: Query method , modification method , Bulk Operation Method , hash and comparison method.
(2) Collection is a root interface that represents a set of objects, also known as collection elements.
(3) There are features in the collection implementation class: Some collection are allowed to repeat elements , some collection are not allowed to repeat , and some collection are ordered ; Some collection are disordered .
(4) The JDK does not provide a direct implementation of the collection interface and provides two more specific interfaces to implement (e.g., java.util.List and Java.util.Set)
----------------------------------------------------------------------------------
Let 's look at the specific methods in java.util.collection<e> :
From the table below you can see that there are 15 methods in the Java.util.collection<e> interface, of which the query operation 6 , modify the operation of 2; batch operation of 5 ; Compare and hash operations of 2 .
| Query operations |
int size () |
Returns the number of elements in this collection. If this collection contains more elements than integer.max_value, integer.max_valueis returned. |
| Boolean IsEmpty () |
Returns True if this collection contains no elements, otherwise false
|
| Boolean contains (Object o) |
Returns Trueif this collection contains the specified element. More precisely, returns truewhen and only if this collection contains at least one element e that satisfies (o==null? E==null:o.equals (e)) . |
| Iterator<e> Iterator () |
Returns an iterator that iterates over the elements of this collection. There is no guarantee as to the order in which elements are returned (unless this collection is a class instance that provides a guaranteed order). |
| Object[] ToArray () |
Returns an array that contains all the elements in this collection. If collection makes some guarantees about the order of elements returned by its iterators, the method must return the elements in the same order. |
| <T> t[] 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. If the specified array can hold the collection, the array containing this collection element is returned. Otherwise, a new array with the run-time type of the specified array and this collection size is assigned. |
| Modify Operation |
Boolean Add (E E) |
Adds the element e to this collection, returns True if the add succeeds, and returns false if the addition fails |
| Boolean remove (Object o) |
Removes a single instance of the specified element o from this collection, returns True if the operation succeeds, and returns false if the operation fails |
| Bulk operations |
Boolean containsall (collection<?> c) |
Returns Trueif this collection contains all the elements in the specified collection. |
| AddAll (collection<? extends e> c) |
Adds all the elements in the specified collection to this collection. |
| Boolean RemoveAll (collection<?> c) |
Removes all elements in this collection that are also contained in the specified collection. |
| Boolean retainall (collection<?> c) |
Only those elements in this collection that are also contained in the specified collection are preserved. |
| void Clear () |
Removes all elements from this collection. |
| Comparison and hashing operations |
Boolean equals (Object O) |
Compares this collection to the specified object for equality |
| int Hashcode () |
Returns the hash code value for this collection |
----------------------------------------------------------------------------------
Java.util.collection<e> source code as follows: (see below the source code, the definition is very normative, all kinds of operations have-----> at this time should think of its implementation of the class how pathetic, how many ways to achieve it. ~_~)
1 PackageJava.util;2 3 4 Public InterfaceCollection<e>extendsIterable<e> {5 //Query Operations6 7 intsize ();8 9 BooleanisEmpty ();Ten One Booleancontains (Object o); A -Iterator<e>iterator (); - the object[] ToArray (); - -<T>t[] ToArray (t[] a); - + //modification Operations - + BooleanAdd (e e); A at BooleanRemove (Object o); - - - //Bulk Operations - - BooleanContainsall (collection<?>c); in - BooleanAddAll (collection<?extendsE>c); to + BooleanRemoveAll (collection<?>c); - the BooleanRetainall (collection<?>c); * $ voidClear ();Panax Notoginseng - the //Comparison and hashing + A Booleanequals (Object o); the + inthashcode (); -}
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Source (java.util.collection<e>)--