Java _ aggregate system-Description of abstract class interfaces related to Map framework and source code -- 08
Abstract: The implementation of Set is based on Map, so you can understand Map first before you can understand Set. Otherwise, getting Set directly will make you feel like it is a waste of time. This section describes the functions of Map related interfaces and abstract classes.
I. Overall Map Framework
Simple Description:
1. a dotted line with no dependency is directly implemented.
2. dotted lines with dependencies, indicating that such dependencies and interfaces are not directly implemented.
3. The solid line is the inheritance relationship, class inheritance class, and interface inheritance interface.
Although the introduction below is about the definition of abstract classes and interfaces, I still feel that the introduction from the source code is more intuitive to illustrate the problem, but also to deepen the understanding of the system, of course, the API is not given again and separate the APIs in the interface from the new APIs in the subclass when introducing its specific subclass.
Ii. Map
1. Interface introduction:
A) store data in the form of key-value pairs. Each key must correspond to a unique value. Duplicate keys are not allowed. Whether the key is allowed to be null depends on the subclass.
B) whether the stored elements are ordered, depending on the sub-classes, such as HashMap unordered, Hashtable ordered, and sorting rules can be sorted by nature or by specified sorting methods.
C) three methods can be used to obtain and iterate the elements in Map, obtain the key set, obtain the value set, and obtain the key-value entity.
D) the subclass must provide two constructor Methods: An empty constructor and a constructor that contains all key-value pairs in the specified input Map structure. all key-value pairs are copied as their own key-value pairs ).
2. source code analysis:
Package com. chy. collection. core; public interface Map
{// Query Operations/** returns the number of all mappings */int size ();/** whether ing exists */boolean isEmpty (); /** whether to include the key with the key value */boolean containsKey (Object key);/** whether to include the value with the value */boolean containsValue (Object value ); /** get value based on the specified key */V get (Object key ); // Modification Operations/** put a key-value pair into the Map and return the previous value associated with the key */V put (K key, V value ); /** delete a key-value Pair Based on the input key and return the value mapped to the deleted key */V remove (Object key ); // Bulk Operations/** put all mappings in the specified Map into the current Map */void putAll (Map
M);/** Delete All mappings in the current Map */void clear (); // Views/** get the Set composed of all keys in the Map */Set
KeySet ();/** get the Collection records of all value groups in the Map */Collection
Values ();/** get the object class Map. Entry consisting of all mappings in the Map
Set */Set
> EntrySet ();/** is a Map key-value pair. It is used only when Iterator */interface Entry
{/** Get the key of the current ing */K getKey ();/** get the value of the current ing */V getValue (); /** set the value of the current ing */V setValue (V value);/** determines whether the current Entry is equal to the input Object */boolean equals (Object o ); /** obtain the hash value of the current Entry */int hashCode ();} // Comparison and hashing/** if m1.entrySet (). equals (m2.entrySet (), the two mappings m1 and m2 indicate the same ing relationship. */Boolean equals (Object o);/** return the hash value of this ing */int hashCode ();}
Simple Description: The methods in Map source code can be divided into query and modification (including adding and deleting mappings in Map) obtain the view and an internal interface that is used only when Iterator defines the method for each ing after operation iteration-Get key, get value, and modify ing value. Source code ==========================
Iii. AbstractMap
1. abstract class introduction:
A) AbstractMap implements the Map interface, provides implementation of some methods, and requires all implementation classes that need to implement the Map interface to inherit from AbstractMap and greatly simplify programming.
B) The simple implementation of methods in AbstractMap is centered on the first method to obtain the view. > EntrySet (); obtain entrySet, and then obtain the Iterator of entrySet,
C) the remaining two methods for obtaining a view are through two anonymous classes: new AbstractSet. (), NewAbstractCollection .
D) There are two internal classes that maintain key-value pairs.
2. source code analysis:
Package com. chy. collection. core; import java. util. Iterator; import java. util. Map. Entry; public abstract class implements actmap
Implements Map
{/** Default constructor for sub-classes */protected AbstractMap () {}// Query Operations/** returns the current Map ing count */public int size () {return entrySet (). size ();}/** whether the current ing is blank */public boolean isEmpty () {return size () = 0 ;} /** determine whether there is a value ing in the current Map */public boolean containsValue (Object value) {Iterator
> I = entrySet (). iterator (); if (value = null) {while (I. hasNext () {Entry
E = I. next (); if (e. getValue () = null) return true ;}} else {while (I. hasNext () {Entry
E = I. next (); if (value. equals (e. getValue () return true;} return false;}/** determines whether there is a key-to-key ing in the current Map */public boolean containsKey (Object key) {Iterator
> I = entrySet (). iterator (); if (key = null) {while (I. hasNext () {Entry
E = I. next (); if (e. getKey () = null) return true ;}} else {while (I. hasNext () {Entry
E = I. next (); if (key. equals (e. getKey () return true;} return false;}/** get the key value */public V get (Object key) {Iterator
> I = entrySet (). iterator (); if (key = null) {while (I. hasNext () {Entry
E = I. next (); if (e. getKey () = null) return e. getValue () ;}} else {while (I. hasNext () {Entry
E = I. next (); if (key. equals (e. getKey () return e. getValue () ;}} return null ;} // Modification Operations/** put a ing into Map, requiring sub-classes to have their own implementations */public V put (K key, V value) {throw new UnsupportedOperationException ();}/** delete key-to-key ing */public V remove (Object key) {Iterator
> I = entrySet (). iterator (); Entry
CorrectEntry = 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 = I. next (); if (key. equals (e. getKey () correctEntry = e ;}} V oldValue = null; if (correctEntry! = Null) {oldValue = correctEntry. getValue (); I. remove ();} return oldValue;} // Bulk Operations/** put all key-value pairs in the specified Map into the current Map */public void putAll (Map
M) {for (Map. Entry
E: m. entrySet () put (e. getKey (), e. getValue ();}/** clear current Map */public void clear () {entrySet (). clear ();} // Views transient volatile Set
KeySet = null; transient volatile Collection
Values = null;/** get the Set */public Set consisting of all the keys in the current Map
KeySet () {if (keySet = null) {// obtain the keySet object class keySet = new AbstractSet through the Anonymous class
() {// Implement the Iterator acquisition method and implement the Iterator internal method public Iterator
Iterator () {return new Iterator
() {Private Iterator
> I = entrySet (). iterator (); public boolean hasNext () {return I. hasNext ();} public K next () {return 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 ;}/ ** get the set of all values of the current Map */public Collection
Values () {if (values = null) {// obtain the Collection object class values = new AbstractCollection using an anonymous class
() {// Implement the Iterator acquisition method and implement the Iterator internal method public Iterator
Iterator () {return new Iterator
() {Private Iterator
> I = entrySet (). iterator (); public boolean hasNext () {return I. hasNext ();} public V next () {return 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 ;}/ ** get the Set */public abstract Set composed of all mappings in the current Map
> EntrySet (); // Comparison and hashing public boolean equals (Object o) {// if it is itself, return trueif (o = this) return true; // returns falseif (! (O instanceof Map) return false; Map
M = (Map
) O; // optimization operation. if the size of the input Map is different from the size of the current Map, it can be determined that the two maps are not equal if (m. size ()! = Size () return false; // compare all values in the two maps to be equal. If they are equal, true is returned. If they are not equal, false is returned. try {Iterator
> I = entrySet (). iterator (); while (I. hasNext () {Entry
E = I. next (); K key = e. getKey (); V value = e. getValue (); if (value = null) {if (! (M. get (key) = null & m. containsKey (key) return false;} else {if (! Value. equals (m. get (key) return false ;}} catch (ClassCastException unused) {return false;} catch (NullPointerException unused) {return false;} return true ;} /** return the hash value of the ing. */Public int hashCode () {int h = 0; Iterator
> I = entrySet (). iterator (); while (I. hasNext () h + = I. next (). hashCode (); return h;}/** returns the string representation of this ing. */Public String toString () {Iterator
> I = entrySet (). iterator (); if (! I. hasNext () return "{}"; StringBuilder sb = new StringBuilder (); sb. append ('{'); for (;) {Entry
E = I. next (); K key = e. getKey (); V value = e. getValue (); sb. append (key = this? "(This Map)": key); sb. append ('='); sb. append (value = this? "(This Map)": value); if (! I. hasNext () return sb. append ('}'). toString (); sb. append (",") ;}}/** returns the clone value of the current Map */protected Object clone () throws CloneNotSupportedException {AbstractMap
Result = (AbstractMap
) Super. clone (); result. keySet = null; result. values = null; return result;}/** compare whether two objects are equal */private static boolean eq (Object o1, Object o2) {return o1 = null? O2 = null: o1.equals (o2);} // Implementation Note: SimpleEntry and SimpleImmutableEntry // are distinct unrelated classes, even though they share // some code. since you can't add or subtract final-ness // of a field in a subclass, they can't share representations, // and the amount of duplicated code is too small to warrant // exposing a common abstract class. /** maintain the key and value Entry. This class supports setValue */public static class SimpleEntry.
Implements Entry
, Java. io. serializable {private static final long serialVersionUID =-8499721149061103585L; private final K key; private V value; public SimpleEntry (K key, V value) {this. key = key; this. value = value;} public SimpleEntry (Entry
Entry) {this. key = entry. getKey (); this. value = entry. getValue () ;}public K getKey () {return key;} public V getValue () {return value;} public V setValue (V value) {V 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 () {return (key = null? 0: key. hashCode () ^ (value = null? 0: value. hashCode ();} public String toString () {return key + "=" + value ;}} /** the Entry that maintains the unchangeable key and value does not support the setValue method. Generally used in multi-threaded environments */public static class SimpleImmutableEntry
Implements Entry
, Java. io. serializable {private static final long serialVersionUID = 7138329143949025153L; private final K key; private final V value; public SimpleImmutableEntry (K key, V value) {this. key = key; this. value = value;} public SimpleImmutableEntry (Entry
Entry) {this. key = entry. getKey (); this. value = entry. getValue () ;}public K getKey () {return key;} public V getValue () {return value;} public V setValue (V value) {throw new UnsupportedOperationException ();} 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 () {return (key = null? 0: key. hashCode () ^ (value = null? 0: value. hashCode ();} public String toString () {return key + "=" + value ;}}}
4. SortedMap
1. Interface introduction:
A) further provide Map on the overall sorting of keys. The ing is sorted based on the natural order of its keys, or based on the Comparator provided during the creation of the ordered ing.
B) The Comparable interface must be implemented to insert all the keys of the ordered ing, and all these keys can be compared.
C) The internal methods are defined around the sorting of keys. They are summarized as follows: (1) obtain the submap greater than or less than the specified key, and (2) obtain the subset Map in the specified range of the key. Obtain the first, last, and (3) view methods. (4) obtain the sorting comparator of the current set.
2. source code analysis:
Package com. chy. collection. core; import java. util. Comparator; public interface SortedMap
Extends Map
{/** Returns the comparator for sorting the keys in this ing. If this ing uses the natural sequence of keys, null is returned. */Comparator
Comparator ();/** returns some views of this ing. The key value ranges from fromKey (included) to toKey (not included ). */SortedMap
SubMap (K fromKey, K toKey);/** return some views of this ing, whose key value is strictly less than toKey. */SortedMap
HeadMap (K toKey);/** returns some views of this ing, whose keys are greater than or equal to fromKey. */SortedMap
TailMap (K fromKey);/** returns the current first (lowest) key in this ing. */K firstKey ();/** returns the current first (lowest) key in this ing. */K lastKey ();/** return the Set view of the keys contained in this ing. */Set
KeySet ();/** return the Collection view of the values contained in this ing. */Collection
Values ();/** returns the Set view of the ing relationships contained in this ing. */Set
> EntrySet ();}
V. NavigableMap
1. Interface introduction:
A) is an extension of the SortedMap interface,
B) MethodlowerEntry,floorEntry,ceilingEntryAndhigherEntryReturn the keys associated with keys smaller than, less than or equal to, greater than or equal to, and greater than the given keys respectively.Map.EntryObject.
C) MethodlowerKey,floorKey,ceilingKeyAndhigherKeyOnly the associated keys are returned.
D) You can access and traverse keys in ascending or descending order.NavigableMap
E)subMap,headMapAndtailMapMethod and name are similarSortedMapThe difference between the method is that it can accept additional parameters used to describe whether to include (or not) the lower boundary and the upper boundary.
F)firstEntry,pollFirstEntry,lastEntryAndpollLastEntryMethod, they return and/or remove the least and maximum ing relationships (if any), otherwise returnnull.
2. source code analysis:
Package com. chy. collection. core; import java. util. NavigableSet; public interface NavigableMap
Extends SortedMap
{/** Returns a key-value ing relationship, which is associated with the maximum key Strictly less than the given key. If such a key does not exist, null is returned. */Map. Entry
LowerEntry (K key);/** returns the maximum key Strictly less than the given key. If such a key does not exist, null is returned. */K lowerKey (K key);/** returns a key-value ing relationship associated with the smallest key in the ing. If the ing is null, null is returned. */Map. Entry
FloorEntry (K key);/** returns the maximum key less than or equal to the given key. If such a key does not exist, null is returned. */K floorKey (K key);/** return a key-value ing relationship, which is associated with the minimum key greater than or equal to the given key. If such a key does not exist, returns null. */Map. Entry
CeilingEntry (K key);/** returns the minimum key greater than or equal to the given key. If such a key does not exist, null is returned. */K ceilingKey (K key);/** return a key-value ing relationship, which is associated with the minimum key strictly greater than the given key. If such a key does not exist, returns null. */Map. Entry
HigherEntry (K key);/** returns the minimum key strictly greater than the given key. If such a key does not exist, null is returned. */K higherKey (K key);/** returns a key-value ing relationship associated with the smallest key in the ing. If the ing is null, null is returned. */Map. Entry
FirstEntry ();/** returns a key-value ing relationship, which is strictly less than the maximum key of the given key. If such a key does not exist, null is returned. */Map. Entry
LastEntry ();/** remove and return the key-value ing relationship associated with the smallest key in the ing. If the ing is null, null is returned. */Map. Entry
PollFirstEntry ();/** remove and return the key-value ing relationship associated with the maximum key in this ing. If the ing is empty, null is returned. */Map. Entry
PollLastEntry ();/** return the reverse view of the ing in this ing. */NavigableMap
DescendingMap ();/** return the NavigableSet view of the key contained in the ing. */NavigableSet
NavigableKeySet ();/** return the backward NavigableSet view of the keys contained in the ing. */NavigableSet
DescendingKeySet ();/** return some views mapped to the map. The key range is from fromKey to toKey. */NavigableMap
SubMap (K fromKey, boolean fromInclusive, K toKey, boolean toInclusive);/** returns some views mapped to, whose keys are smaller than (or equal to, if the SIVE sive value is true) toKey. */NavigableMap
HeadMap (K toKey, boolean partial SIVE);/** returns some views mapped to fromKey, whose keys are greater than (or equal to, if the inclusive value is true. */NavigableMap
TailMap (K fromKey, boolean writable SIVE);/** return some views of this ing. The key value ranges from fromKey (included) to toKey (not included ). */SortedMap
SubMap (K fromKey, K toKey);/** return some views of this ing, whose key value is strictly less than toKey. */SortedMap
HeadMap (K toKey);/** returns some views of this ing, whose keys are greater than or equal to fromKey. */SortedMap
TailMap (K fromKey );}
More content: java _ overall directory of the collection system-00