An inefficient Map Implementation: AbstractMap
AbstractMap Map provides for the realization of default, but in sub-categories are generally covered, because here are relatively low efficiency of the realization of this type of method to achieve almost all of what value is only realized!
The following statement:
Public abstract class AbstractMap implements Map
The Map:
The realization of this type are relatively simple, almost all through entrySet completed, so basically have done nothing of these codes can be used as reference:
Public int size () (
Return entrySet (). Size ();
)
Public boolean isEmpty () (
Return size () == 0;
)
Public boolean containsValue (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 (e.getValue ()))
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 (e.getKey ()))
Return true;
)
)
Return false;
)
Public Object get (Object key) (
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 correctEntry = null;
If (key == null) (
While (correctEntry == null & i.hasNext ()) (
Entry e = (Entry) i.next ();
If (e.getKey () == null)
CorrectEntry = e;
)
Else ()
While (correctEntry == null & i.hasNext ()) (
Entry e = (Entry) i.next ();
If (key.equals (e.getKey ()))
CorrectEntry = e;
)
)
Object oldValue = 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 iterator () (
Return new Iterator () (
EntrySet private Iterator i = (). Iterator ();
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;
)
Here is the key to return to the Set, this method there are two categories of anonymity, relatively poor readability, is a subclass of AbstractSet then this sub-class of iterator () method has generated an anonymous categories: achieving Iterator interface If the relatively anonymous familiar, it should be no problem.
Public Collection values () (
If (values == null) (
Values = new AbstractCollection () (
Public Iterator iterator () (
Return new Iterator () (
EntrySet private Iterator i = (). Iterator ();
Public boolean hasNext () (
Return i.hasNext ();
)
Public Object next () (
Return ((Entry) i.next ()). GetValue ();
)
Public void remove () (
I.remove ();
)
);
)
Public int size () (
Return AbstractMap.this.size ();
)
Public boolean contains (Object v) (
Return AbstractMap.this.containsValue (v);
)
);
)
Return values;
)
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 (key)))
Return false;
)
)
) Catch (ClassCastException unused) (
Return false;
) Catch (NullPointerException unused) (
Return false;
)
Return true;
)
Public int hashCode () (
Int h = 0;
Iterator 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 (hasNext) (
Entry e = (Entry) (i.next ());
Object key = e.getKey ();
Object value = e.getValue ();
Buf.append ((key == this? "(This Map)": key) + "=" + (value == this? "(This Map)": value));
HasNext = i.hasNext ();
If (hasNext)
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;
)
Also declared a static internal category, but not used here:
Static class SimpleEntry implements 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));
)
)
Tags: AbstractMap, Map






