AbstractCollection source code analysis

xiaoxiao2021-03-06  57

AbstractCollection abstraction provides Collection's skeleton implementation, please: http://blog.9cbs.net/treeroot/admin/referRers.aspx? Entryid = 99591 Here directly looks its code how to implement .public Abstract Iterator Iterator (); this method does not implement .public Abstract Int size (); this method does not implement .public boolean == 0;} is very simple, directly call the size () method Return the size, if it is 0 I think it is empty collection. You don't think weird, how can the size () method don't be implemented? Because this is an abstract class, it is not possible to instantiate, and when it is called the realization in the subclass. Public Boolean Contains (Object O) {Iterator E = Iterator (); if (o == Null) {while (E.HASNext ()) IF (E.NEXT () == Null) Return True;} else {while E.hasNext ()) f (} (E.Next ())) Return True;} Return False;} The code is not complicated, but also calls your own method Iterator, traversal collection, if you find it, return True, no It is necessary to traverse the entire collection. For the null value, it must be put in the IF instead of the else .public object [] to () {Object [Size ()] Iterator E = Iterator (); for (int i = 0; E.hasNext (); i ) Result [i] = E.NEXT (); return result;} Returns an array, gentleman is a set size , Then assign a value by traversing .public object [] toarray (object a []) {int size = size (); if (a.length

Iterator it = item (); for (int i = 0; i

a [i] = it.next ();

IF (a.length> size) a [size] = null;

RETURN A;} This method If the array A is smaller than the set, it produces a new array in this case, where reflection (can be seen from the literal). Why can't you Direct NEW? Because the elements in array A are running time type, not just Object. It can be seen that when A can accommodate the entire collection, and if A is large, a bit is large, will set a [size] is a null value, and only this one is set. If the collection allows the NULL value, it is impossible to determine the size of the collection by returning an array, because the last few of the possible collections is NULL value .public Boolean Add (Object O ) {Throw new unsupportedoperationException ();} This method provides implementation, but it is directly throwing an exception, and the unrealized method is different. Non-abstract subclasses may not rewrite this method, if subclasses don't want to support If this operation, the unrealized method subclass must be implemented, otherwise it is compiled with .public boolean remove (object o) {itrator E = item (); if (o == null) {while (E.hasNext ()) {IF (E.Next () == null) {E.Remove (); return true;}}} else}} else {while (E.hasNext ()) {i (ney ") {.ext ())) { E.Remove (); return true;}}} Return False;} This method is basically the same as the Contians method, and there is 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 relatively simple, but the time complexity is M * n, and if there is no element in the collection, if one is not directly returned FALSE. It can be seen that the time that returns True is more than returning false. More .public boolean addall (color c) {boolean modified = false; item e = c.Iterator (); while (e.hasnext ()) {if (add (e.next ())) modified = true;} return This is a collection of adds a collection. The same here is not implemented. To note that the point is to return True as long as the at least one element function is added, indicating that the original collection changes .public Boolean Removeall ( Collection c) {boolean modified = false; item e = item (); while (e.hasNext ()) {if (c.contains ())) {E.Remove (); modified = true; } Return Modified;} This method judges each element in the collection. If you delete it in the collection C, the same value will be deleted, and the at least one element will return to True.Public Boolean Retainal (Collection C) {Boolean Modified = false; item e = iterator (); while (E.hasNext ()) {if (! c.contains (E.NEXT ())) {E.Remove (); Modified = true;}} Return Modified; } This method is almost the same, there is more than one is not (!). Public void clear () {iplic void clear () {iplic (E.hasNext ()) {E.Next (); E.Remove () }} This method clears the collection, but this method is more efficient, and the emptying should not be traversed, but the subclasses can be heavy. Write the entire method .public string toString () {stringbuffer buf = new stringbuffer (); buf.append ("["); item i = item (); boolean hasnext = i.hasnext (); while (havenex) {Object O = i.next (); buf.Append (== this collection ": string.valueof (o)); hasnext = I.hasnext (); if (Hasnext) buf.append (", " }

BUF.Append ("]"); return buf.tostring ();} The last method is to bring all the elements to [] to return, ",", ",", "here, the only payment can be included Yourself, if it is not judged, it will become cycling.

转载请注明原文地址:https://www.9cbs.com/read-117985.html

New Post(0)