JAVA, Map interface, Iterator, mapiterator
1.MapInterface Overview
The java. util. Map interface describes the ing structure. The Map interface allows you to view the ing content in the form of a key set, value set, or key-value ing link set.
Java comes with various Map classes. These Map classes can be categorized into three types:
1. General Map, used to manage mappings in applications, is usually implemented in the java. util package.
* HashMap
* Hashtable
* Properties
* LinkedHashMap
* IdentityHashMap
* TreeMap
* WeakHashMap
* ConcurrentHashMap
2. Dedicated Map. You do not need to create such a Map yourself, but access it through some other classes.
* Java. util. jar. Attributes
* Javax. print. attribute. standard. PrinterStateReasons
* Java. security. Provider
* Java. awt. RenderingHints
* Javax. swing. UIDefaults
3. An abstract class used to help implement your own Map class
* AbstractMap
The important methods in the interface are as follows:
1. covered methods
Equals (Object o) // compare the equivalence between the specified Object and the Map
HashCode () // return the hash code of this Map
2. Map update method. You can change the content of Map.
Put (Object key, Object value) // Add a key-value pair. If the key already exists, the old value is overwritten.
PutAll (Map t) // copy all mappings in the specified Map to this map
Remove (Object key) // Delete the value associated with the key from the Map
Clear () // Delete All mappings from the Map
3. Map Method of the returned view: using these methods to return objects, You can traverse and delete Map elements.
Set keySet () // return the Set view of the keys contained in the Map.
// Deleting the key element in the Set will also delete the corresponding ing (key and value) in the Map)
Collection values () // return the Collection view of values contained in the map.
// Deleting the value element in the Collection will also delete the corresponding ing (key and value) in the Map)
Set entrySet () // return the Set view (key-Value Pair) mapped to the Map contained in the Map ).
Each element in the Set is a Map. Entry object. You can use the getKey () and getValue () methods (and the setValue () method) to access the key elements and value elements of the Map. Entry object.
Map. Entry interface
The entrySet () method of Map returns a set of objects that implement the Map. Entry interface. Each object in the set is a specific key/value pair in the underlying Map. Through the iterator of this set, you can obtain the key or value of each entry (unique acquisition method) and change the value.
(1) Object getKey (): return the keyword of the Entry
(2) Object getValue (): return the value of the Entry
(3) Object setValue (Object value): change the value in the relevant image to value and return the old value.
After an entry is returned through the iterator, unless it is the iterator's own remove () method or the setValue () method of the entry returned by the iterator, other modifications to the source Map will invalidate the entry set, and the entry behavior will be undefined.
4. Map access and Test Methods: these methods retrieve information about the Map content without changing the Map content.
Get (Object key) // returns the value associated with the specified key and the Object. If no value exists, null is returned.
Boolean containsKey (Object key) // returns true if Map contains the ing of the specified key.
Boolean containsValue (Object value) // If this Map maps one or more keys to a specified value, true is returned.
IsEmpty () // returns true if Map does not contain key-value ing.
Int size () // returns the number of key-value mappings in the Map.
Almost all common maps use Hash ing. This is a simple mechanism to Map elements to arrays. you should understand the working principle of hash ing to make full use of Map.
The hash ing structure consists of an internal array of storage elements. Because array storage is used internally, there must be an index mechanism for determining any key to access the array. In fact, this mechanism requires an integer index value (I .e. the remainder) smaller than the array size ). This mechanism is called a hash function. In Java hash-based Map, the hash function converts an object into an integer suitable for an internal array. You don't have to worry about finding an easy-to-use Hash function: each object contains a hashCode () method that returns an integer. To map the value to an array, you only need to convert it to a positive value, and then divide the value by the array size and take the remainder.
The hash function maps any object to an array, but what if two different keys are mapped to the same position? This is an inevitable situation. In terms of hash ing, this is called a conflict. Map can handle these conflicts by inserting a link list at the index location and simply adding elements to the link list.
Figure:
Iterator)
An iterator is a design pattern. It is an object that can traverse and select objects in a sequence. Developers do not need to understand the underlying structure of the sequence. An iterator is usually called a "lightweight" object because it is easy to create.
The Iterator function in Java is relatively simple and can only be moved one way:
(1) The method iterator () requires the container to return an Iterator. When the next () method of Iterator is called for the first time, it returns the first element of the sequence. Note: The iterator () method is a java. lang. Iterable interface inherited by Collection.
(2) Use next () to obtain the next element in the sequence.
(3) Use hasNext () to check whether there are any elements in the sequence.
(4) use remove () to delete the elements returned by the iterator.
Iterator is the simplest implementation of the Java Iterator. The ListIterator designed for List has more functions. It can traverse the List in two directions, or insert and delete elements from the List.
The List interface has a unique method, listIterator.
Disadvantages of using Iterator:
1. ListIterator has the add () method, which can be used to add objects to the List. Iterator cannot
2. both ListIterator and Iterator have hasNext () and next () methods, which can implement sequential backward traversal, but ListIterator has hasPrevious () and previous () methods, which can implement reverse (sequential forward) traverse. Iterator cannot.
3. ListIterator can be used to locate the current index location. nextIndex () and previousIndex () can be used. Iterator does not have this function.
4. All objects can be deleted. However, ListIterator can modify objects and set () can be implemented. Iierator can only be traversed and cannot be modified.