3.6. LinkedHashSet class
LinkedHashSet extension hashset. LINKEDHSHSET implementation will help if you want to track the order of the elements added to havehset. The iterator of the Linkedhashset access each element is accessed according to the insertion order of the element. It provides an ordered collection that can quickly access individual elements. At the same time, it also increases the cost of implementation, because each element in the hash table is linked to a double link list.
(1) LinkedHashSet (): Build an empty link hash
(2) LinkedHashSet: Build a link hash set and add all elements in the collection C.
(3) LinkedHashSet (int initialcapacity): Build an empty link with a specific capacity has a hash set
(4) LinkedHashSet (Int InitialCapacity, Float LoadFactor): Build an empty chain terminal with a specific capacity and load factor. LoadFactor is a number between 0.0 to 1.0
"To optimize the use of Hashset space, you can tune the initial capacity and load factor. The Treeset does not contain tuning options because the tree is always balanced. "
4. MAP interface
The MAP interface is not inheritance of the Collection interface. The MAP interface is used for maintenance key / value pairs (Key / Value PAIRS). This interface describes the mapping from the non-repeated key to the value.
(1) Add, delete the operation:
Object Put (Object Key, Object Value): Place a keyword associated with each other with a value in the image. If the keyword already exists, the new value related to this keyword will replace the old value. Method Returns the old value of the keyword, returns NULL if the keyword does not exist
Object Remove (Object Key): Remove from the image with key-related mapping
Void Putall (MAP T): Add all elements from a particular image to this image
Void Clear (): Remove all mappings from the image
"The keys and values can be NULL. However, you cannot add MAP as a key or value to itself. "
(2) Query operation:
Object GET (Object Key): Get values associated with the keyword key and returns objects related to keyword key, return NULL if you do not find this key in this image.
Boolean ContainsKey: Decluders if there is a keyword key
Boolean ContainSValue (Object Value): Determines if there is a value Value in the image
INT size (): Returns the number of maps in the current image
Boolean ISempty (): Decluders if there is any mapping in the image
(3) View operation: Processing a key / value pair
Set KeySet (): Back to the image of all keywords in the image
"Because the collection of keywords must be unique, you support it with SET. You can also remove elements from the view, while the keywords and its associated values are deleted from the source image, but you can't add any elements. "
Collection Values (): Returns the view set of all values in the image
"Because the collection of values in the mapping is not unique, you support it with Collection. You can also remove elements from the view, and the values and its keywords are deleted from the source image, but you can't add any elements. "
Set entryset (): Returns the view set of map.Entry objects, namely the keyword / value pair in the image
"Because the mapping is unique, you support it with SET. You can also remove elements from the view, while these elements will be deleted from the source image, but you can't add any elements. "
4.1. Map.Entry interface
Map's entryset () method returns an object collection that implements the map.Entry interface. Each object in the collection is a specific key / value pair in the underlying Map. Through this collection iterator, you can get the keys or values of each entry (unique acquisition mode) and make changes. When the entry returns through the iterator, unless it is a setValue () method returned by the iterator itself, the remaining MAP external modifications will cause this set to be invalid, while generating entry behavior Not defined.
(1) Object getKey (): Returns the keywords for the entry
(2) Object getValue (): Returns the value of the entries
(3) Object setValue (Object value): change the value in the related image to Value and return the old value
4.2. SortedMap interface
"Collection Framework" provides a special MAP interface: sortedMap, which is used to hold the orderly order of the keys.
The SortedMap interface is a view (subset) of the image, including two endpoints provide an access method. In addition to the sorting is the key acting on the mapping, SortedMap is processed, and the SortedSet is processed.
Adding an element added to the sortedMap implementation class must implement the Comparable interface, otherwise you must provide a constructor to provide a COMPARATOR interface implementation. The TreeMap class is the only implementation thereof.
"Because for the mapping, each key can only correspond to one value, if two keys generate 0 return values (by comparable compareto () method if the two keys are compared (by comparable compareto () method or through the Compare () method ), Then the original key is replaced by the new value. If the two elements are equal, it is ok. But if you don't wait, then you should modify the comparison method, which makes the comparison method and equals (). "
(1) Comparator Comparator (): Returns the comparator used when sorting the keyword, returns NULL if the keyword is compared using the CompareTo () method of the Comparable interface.
(2) Object firstkey (): Returns the first (minimum) keyword in the image
(3) Object lastkey (): Returns the last (highest) keyword in the image
(4) SortedMap Submap (Object fromkey, Object Tokey): Returns sortedMap view from fromKey to ToKey (not included) (subset)
(5) SortedMap Headmap: Returns a view of SortedMap, and the keys of each element are smaller than Tokey
(6) Sorted TailMap (Object fromkey): Returns a view of SortMap, and the key of each element is greater than or equal to fromkey.
4.3. Abstractmap Abstract Class
Similar to other abstract sets, the AbstractMap class covers the equals () and havehcode () methods to ensure that two phase-like mappings return the same hash code. If the two mappings are equal, the same key is included and each key has the same value in these two mappings, and the two mappings are equal. The mapping hash code is the sum of the map element hash code, each of which is an implementation of the map.Entry interface. Therefore, regardless of how the internal order is mapped, two equal mappings report the same hash code.
4.4. HashMap class and Treemap class
"Collection Framework" offers two conventional MAP implementations: HashMap and Treemap (Treemap implementation SortedMap interface). Insert, delete, and locate elements in Map, HashMap is the best choice. But if you want to traverse keys in natural order or custom order, TreeMap is better. The key class that is added using HashMap requires explicitly defines the implementation of HashCode () and Equals (). This TreeMap has no tuning options because the tree is always equilibrium.
4.4.1. Hashmap class
To optimize the use of HashMap space, you can tune the initial capacity and load factors.
(1) HashMap (): Build an empty hash image
(2) HashMap (MAP M): Build a hash image and add all mappings of image M
(3) Hashmap (int initialcapacity): Build an empty hash image with a specific capacity
(4) Hashmap (int initialcapacity, float loadfactor): Build an empty hash image with specific capacity and loading factors
4.4.2. TreeMap class
TreeMap has no tuning option because the tree is always equilibrium.
(1) TreeMap (): Build an empty image tree
(2) TreeMap (Map M): Build an image tree and add all the elements in the image m
(3) TreeMap: Build an image tree and sort the keyword using a specific comparator
(4) TreeMap (SortedMap S): Build an image tree, add all mappings in the image tree S, and use the same comparator as the ordered image S
4.5. LinkedHashMap class
LINKEDHASHMAP Extended HashMap, insert the keyword / value to add a link hash image in the order. Like LinkedHashSet, LinkedHashMap is also used in a dual link list.
(1) LinkedHashMap (): Build an empty link hash image
(2) LinkedHashMap (MAP M): Build a link hash image and add all mappings in the image m
(3) LinkedHashMap (int initialcapacity): Build a empty link hash image with a specific capacity
(4) LinkedHashmap (int initialcapacity, float loadfactor): Build an empty link hash image with specific capacity and loading factors
(5) LinkedHashmap (int initialcapacity, float loadfactor,
Boolean AccessOrder: Build an empty link hash image with specific capacity, loading factors, and access order
"If the AccessOrder is set to True, the link hash image will use the access order instead of insertion order.
Alternative to each image. Each time you call the GET or PUT method, the relevant mapping is deleted from its current location, then place it at the end of the link image list (only the location in the list of link images will be affected, hash table Not affected. The hash table map always stays in the hash of the hash code corresponding to the keyword. "
"This feature is useful for" deleting the most recently used "in cache. For example, you can save the most commonly accessed mapping in memory and read objects that are not accessed frequently from the database. When you can't find a map in the table, and the mapping in the table has already been very full, you can let the iterator enters the table, and remove the beginning of the beginning of the enumeration. These are the most recently used mappings. "
(6) Protected Boolean Removeeldestentry: If you want to delete the oldest mapping, you can override this method to return True. This method is adjusted after a mapping has been added to the image. Its default implementation method returns false, indicating that the old mapping under the default condition is not deleted. But you can redefine this method so that you can choose a certain condition in the oldest mapping, or the image exceeds a certain size, returns True. 4.6. WeakhashMap class
WeakhashMap is a special implementation of MAP, which uses WeakReference (weak reference) to store the hash table keyword. When using this way, when the mapping key is no longer referenced outside WeakHashMap, the garbage collector will reclaim it, but it will incorporate weak references to the object into a queue. Weakhashmap runs regularly checks the queue to find new weakened applications. When a weak reference arrives at the queue, it means that the keyword is no longer used by anyone, and it has been collected. Then WeakhashMap deletes the relevant mapping.
(1) WeakhashMap (): Build an spaceless hash image
(2) WeakhashMap (MAP T): Build a weak hash image and add all mappings in the image T
(3) WeakhashMap (int initialcapacity): Build an empty weaker brightening with specific capacity
(4) WeakhashMap (INT InitialCapacity, Float LoadFactor): Build an empty weak haveh image with a specific capacity and load factor
4.6. Identityhashmap class
Identityhashmap is also a special implementation of MAP. In this class, the hash code of the keyword should not be calculated by the HashCode () method, but should be calculated by the System.Identityhashcode method (even if the HashCode method has been redefined). This is the method used by Object.hashcode to calculate the hash code based on the memory address of the object. In addition, in order to compare individual objects, IdentityHashMap will use ==, without using the equals method.
In other words, different keyword objects are also considered different objects even if their content is the same. The IdentityHashMap class can be used to implement Topology-PreserVing Object Graph Transformations (such as serialization or depth copy of objects), when converting, requires a "node table" to track those objects that have been processed. Quote. Even if there is an object equal, "node table" should not be seen. Another application is to maintain a proxy object. For example, debugging tools want to maintain a proxy object of each object during program debugging.
"Identityhashmap class is not a general significance MAP implementation! Its implementation is intended to violate the MAP interface requirement to compare objects through the Equals method. This class is only used in rare happening, it is necessary to emphasize equivalence semantic. "
(1) IdentityhashMap (): Build an empty whole hash image, the default expectation is 21
"Expected maximum size is the maximum number of key / value mappings that are expected to hold."
(2) IdentityHashMap (MAP M): Build a whole hash image and add all mappings in the image m
(3) Identityhashmap
INT ExpectedMaxSize:
Build an empty whole hash image with the expected maximum size. Place the key than the expected maximum size
/
When the value map, the growth of internal data structures will be caused, sometimes it may take time.