A low efficiency: AbstractMap

xiaoxiao2021-03-06  53

Abstractmap provides the default implementation for MAP, but it is generally covered in subclasses because the achievement efficiency here is relatively low, and the implementation methods in this class have almost no value, just achieved! The declaration is as follows: Public Abstract Class AbstractMap Implements Map Related to Map: http://blog.9cbs.net/treeroot/Archive/2004/09/20/110331.aspx

The implementation of this class is relatively simple, almost all passed through the entryset, so it is basically nothing to do, these code can be used as a reference:

Public int size () {return entryset (). size ();

Public Boolean ISEMPTY () {Return Size () == 0;

Public Boolean ContaSvalue (Object Value) {Iterator i = entryset (). Iterator (); if (value == null) {while (I.hasNext ()) {entry E = (entry) i.next (); if E.GetValue () == null) Return true;}}}} else {while (i.hasNext ()) {entry E = (entry) i.next (); if (value.equals ())) Return true;}} Return False;

Public Boolean Containskey (Object Key) {Iterator i = entryset (). Iterator (); if (Key == Null) {While (I.hasNext ()) {entry E = (entry) i.next (); if ( E.GetKey () == null) Return true;}}} else {while (I.hasNext ()) {entry E = (entry) i.next (); if (key.equals ())) Return true;}} Return False;

Public Object Get {Iterator i = entryset (). Iterator (); if (key == null) {while (I.hasNext ()) {entry E = (entry) i.next (); if ( E.GetKey () == null) Return E.GETVALUE ();}} else {while (i.hasnext ()) {entry E = (entry) i.next (); if (Key.Equals (E.GetKey ()) Return E.GETVALUE ();}} Return NULL;}

Public Object Put (Object Key, Object Value) {throw new unsupportedOperationException ();

Public Object Remove (Object Key) {Iterator i = entryset (). Iterator (); entry correntry = null; if (key == null) {while (CorrecTentry == Null && I.hasNext ()) {Entry E = I.Next (); if (E.GetKey () == null) CorRectentry = E;}}} else {while (CorrecTentry == Null && I.hasnext ()) {entry E = (entry) i.next (); If (en (E.GetKey ()))))) CorrecTentry = E;}} Object}} Object = NULL; if (CorRectentry! = Null) {OldValue = CorrecTentry.getValue (); i.remove ();} Return OldValue;}

Public void Putall (Map T) {Iterator I = T.Entryset (). Iterator (); while (i.hasnext ()) {entry E = (entry) i.next (); PUT (E.GetKey (), E.getValue ());}}

Public void clear () {entryset (). clear ();

Transient volatile set keyset = null; Transient Volatile Collection Values ​​= NULL;

Public set keyset () {if (keyset == null) {keyset = new AbstractSet () {public iterator itrator () {Return new item () {private iterator i = entryset (). items (); item ();

Public Boolean Hasnext () {Return I.hasNext (); PUBLIC Object Next () {Return ((entry) i.next ()). getKey ();} public void remove () {i.remove ();} };

Public int size () {return abstractmap.this.size ();

Public Boolean Contains (Object K) {Return AbstractMap.This.Containskey (k);}}};} Return KeySet;} Returning is the key of Key, this method has two anonymous classes, readability, first A subclass of AbstractSet then this subclass's iTerator () has generated an anonymous class: implementing the Iterator interface, if you are familiar with anonymous classes, there should be no problem.

public Collection values ​​() {if (values ​​== null) {values ​​= new AbstractCollection () {public Iterator iterator () {return new Iterator () {private Iterator i = entrySet () iterator ();. public boolean hasNext () {Return I.hasNext (); PUBLIC Object next () {return ((entry) i.next ()). GetValue ();} public void transove () {i.remove ();}}; Size () {return abstractmap.this.size ();

Public Boolean Contains (Object V) {Return AbstractMap.This.ContainsValue (v);}};

Public Abstract set entryset ();

Public Boolean Equals (Object O) {IF (o == this) Return True;

IF (! (o instanceof map) Return False; MAP T = (MAP) O; IF (t.size ()! = size ()) Return False;

Try {Iterator i = entryset (). iterator (); while (I.hasNext ()) {entry E = (entry) i.next (); object key = E.GetKey (); object value = E.GetValue ); If (value == null) {if (! (T.get (key) == null && t.containskey (key))) Return False;} else {if (! Value.equals (t.get) ))))}}} Catch (classcastexception unused) {return false;} catch (nullpointersexception unused) {return false;}

Return True;}

Public int hashcode () {int h = 0; item i = entryset (). iterator (); while (I.hasNext ()) h = i.next (). hashcode (); return h;}

Public string toString () {stringbuffer buf = new stringbuffer (); buf.append ("{");

Iterator i = entryset (). Iterator (); boolean hasnext = i.hasnext (); while (have ney = (entry); object key = E.GetKey (); Object value = E.GetValue (); buf.append (key == this map) ": key) " = " (value == this map)": value); Hasnext = I.hasNext (); if (havenext) buf.append (",");

BUF.Append ("}"); return buf.tostring ();

Protected Object Clone () throws clonenotsupportedException {AbstractMap Result = (Abstractmap) Super.clone (); result.keyset = null; result.values ​​= null; return result;}

It also declares a static internal class, but it is not used here: static class simpleentry imports entry {Object Key; Object Value;

Public SimpleEntry (Object Key, Object Value) {this.key = key; this.Value = value;}

Public SimpleEntry (Map.Entry E) {this.key = E.getKey (); this.Value = E.GetValue ();

Public Object getKey () {Return Key;}

Public Object getValue () {return value;}

Public Object SetValue (Object Value) {Object OldValue = THIS.VALUE; this.Value = Value; Return OldValue;

Public Boolean Equals (Object O) {if (! (o instanceof map.entry) Return False; map.entry E = (map.entry) O; Return EQ (Key, E.GetKey () && EQ (Value, E.GetValue ());

Public Int Hashcode () {Object V; Return ((key == null)? 0: key.hashcode ()) ^ ((value == null)? 0: value.hashcode ());}

Public string toString () {return key "=" value;

Private Static Boolean EQ (Object O1, Object O2) {RETURN (O1 == NULL? O2 == NULL: O1.Equals (O2));}}

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

New Post(0)