The abstractcollection abstract class provides the skeleton implementation of collection. For details about collection analysis, see:
Http://blog.csdn.net/treeroot/admin/Referrers.aspx? Entryid = 99591
Here we will look at how its code is implemented.
Public abstract iterator ();
This method is not implemented.
Public abstract int size ();
This method is not implemented.
Public Boolean isempty (){
Return size () = 0;
}
It is very simple. directly call the size () method to return the size. If it is 0, it is considered as a null set.
You don't think it's strange. The size () method is not implemented. How can it be called? This is an abstract class and cannot be instantiated. Specifically, the implementation in the subclass is called.
Public Boolean contains (Object O ){
Iterator E = iterator ();
If (O = NULL ){
While (E. hasnext ())
If (E. Next () = NULL)
Return true;
} Else {
While (E. hasnext ())
If (O. Equals (E. Next ()))
Return true;
}
Return false;
}
The code is not complex. It also calls its own method iterator to traverse the set. If it is found, true is returned. If it is not found, the entire set is traversed. NULL values must be placed in if, rather than else.
Public object [] toarray (){
Object [] result = new object [size ()];
Iterator E = iterator ();
For (INT I = 0; E. hasnext (); I ++)
Result [I] = E. Next ();
Return result;
}
Returns an array of the same size as the set, and then returns a value through traversal.
Public object [] toarray (Object A []) {
Int size = size ();
If (A. Length <size)
A = (object []) Java. Lang. Reflect. array. newinstance (
A. getclass (). getcomponenttype (), size );
Iterator it = iterator ();
For (INT I = 0; I a [I] = it. Next ();
If (A. length> size)
A [size] = NULL;
Return;
}
This method is the same if array a is smaller than the set. In this case, a new array is generated, and reflection is used here (literally, we can see it ). why can't we use new directly? The element in array A has a runtime type, not just an object. it can be seen that when a can accommodate the entire set, there is no need to re-allocate space. If a is larger than the set, a [size] is set to null, and only this one is set. if the set allows null values, the size of the set cannot be determined by returning an array, because the last few values of the set may be null.
Public Boolean add (Object O ){
Throw new unsupportedoperationexception ();
}
This method provides implementation, but directly throws an exception, which is different from the unimplemented method. This method can not be overwritten in non-Abstract subclasses, if the subclass does not want to support this operation, the method subclass that is not implemented must be implemented. Otherwise, the compilation is the same as that of the method subclass.
Public Boolean remove (Object O ){
Iterator E = iterator ();
If (O = NULL ){
While (E. hasnext ()){
If (E. Next () = NULL ){
E. Remove ();
Return true;
}
}
} Else {
While (E. hasnext ()){
If (O. Equals (E. Next ())){
E. Remove ();
Return true;
}
}
}
Return false;
}
This method is basically the same as the contians method. There is only one more E. Remove statement.
Public Boolean containsall (collection C ){
Iterator E = C. iterator ();
While (E. hasnext ())
If (! Contains (E. Next ()))
Return false;
Return true;
}
This method is concise, but the time complexity is M * n. Check whether no element is in the set. If one element is not found, false is returned directly. it can be seen that it takes more time to return true than false.
Public Boolean addall (collection C ){
Boolean modified = false;
Iterator E = C. iterator ();
While (E. hasnext ()){
If (add (E. Next ()))
Modified = true;
}
Return modified;
}
Here, we add a set one by one. Similarly, the add method is not implemented here. Note that if at least one element function is added, true is returned, indicates that the original set has changed.
Public Boolean removeall (collection C ){
Boolean modified = false;
Iterator E = iterator ();
While (E. hasnext ()){
If (C. Contains (E. Next ())){
E. Remove ();
Modified = true;
}
}
Return modified;
}
This method is used to determine each element in the set. If it is deleted from set C, all elements with the same value will be deleted. if at least one element is deleted, true is returned.
Public Boolean retainall (collection C ){
Boolean modified = false;
Iterator E = iterator ();
While (E. hasnext ()){
If (! C. Contains (E. Next ())){
E. Remove ();
Modified = true;
}
}
Return modified;
}
This method is almost the same as the above, and there is another non (!).
Public void clear (){
Iterator E = iterator ();
While (E. hasnext ()){
E. Next ();
E. Remove ();
}
}
This method clears the set, but the efficiency of this method is relatively low. To clear the set, you do not need to traverse the set. However, subclass can override the entire method.
Public String tostring (){
Stringbuffer Buf = new stringbuffer ();
Buf. append ("[");
Iterator I = iterator ();
Boolean hasnext = I. hasnext ();
While (hasnext ){
Object o = I. Next ();
Buf. append (O = This? "(This collection)": String. valueof (o ));
Hasnext = I. hasnext ();
If (hasnext)
Buf. append (",");
}
Buf. append ("]");
Return Buf. tostring ();
}
The last method is to enclose all elements in [] and return them. The elements are separated. the only note here is that the set can contain itself. If no judgment is made, it will become an endless loop.