JAVA programming ideology (Fourth Edition) Study Notes ---- 11.10 Map, programming ideology ---- 11.10

Source: Internet
Author: User

JAVA programming ideology (Fourth Edition) Study Notes ---- 11.10 Map, programming ideology ---- 11.10

Previously, we learned the List interface at the Collection level. The List hierarchy is relatively simple. Except for the CoppyOnWriteArrayList class related to multi-thread security, this class is used to learn more when it involves multi-thread-related knowledge, in addition to outdated Vector <T> and Stack <T>, there are only two commonly used List interfaces, that is, two classes: array-based ArrayList and linked list-based sort list. The simplest and most commonly used list implementation is ArrayList. As for the queue list, because the Deque queue interface is implemented in its specific implementation, it has more functions than the list. Therefore, it is reasonable to learn the queue list after learning the queue.

In normal development, because the queue is used less often, learning about the queue is also placed behind. Another common container in Collection is the implementation class of the Set interface. Because java implements the Class Based on Map when designing the Set implementation, that is, there is no Set without Map, so we will first learn the Map and then the Set, this learning sequence should be reasonable.

Next, we will first put the relevant level structure of the Map interface (the relevant resources come from the network ):

The most common implementation classes in the Map interface are HashMap (unordered, fast access element), LinkedHashMap (sorted by adding order, fast access), and TreeMap (sorted by key comparison in ascending order)

 

Interface Map <K, V>

public interface Map<K,V> 

The Map <K, V> interface is the root interface at the Map level. Unlike the Collection <T> interface, this interface is a real root interface and is not extended from other interfaces.

Map provides an object that maps keys to values. A ing cannot contain duplicate keys. Each key can only be mapped to one value at most (but the value type can be single-value, such as String, it can also be a multi-value type such as List ). The Map interface provides three collection views, allowing you to view a ing in the form of a key set, value set, or key-value pair. The ing order is defined as the order in which the iterator returns its elements in the ing collection view. Some ing implementations can clearly ensure their order, suchTreeMap, LinkedHashMapClass; other ing implementations do not guarantee the order, suchHashMapClass.

Some ing implementations have limits on possible keys and values. For example, some implementations disable null keys and values, while others impose restrictions on the type of their keys.

 

Abstract class AbstractMap <K, V>

public abstract class AbstractMap<K,V> implements Map<K,V>

This class provides the backbone implementation of the Map interface, and its function is similar to the AbstractCollection in the Collection interface, all of which are required to implement the corresponding interface to the maximum extent. This class does not implement the entrySet () method, and limits the put (K key, V value) method.

To implement non-modifiable ing, you only need to extend the AbstracMap class and provide the entrySet method implementation to implement the set view of the ing relationship between the return key-value. Generally, the returned set will be implemented on AbstractSet (the backbone implementation of the Set Interface) in sequence. This set does not support the add or remove method, and its iterator does not support the remove Method. (What do you mean ???)

To implement modifiable ing, in addition to implementing the entrySet method, you also need to implement the put method of the AbstractMap class. This class imposes restrictions on this method. If this class is not overridden or thrownUnsupportedOperationException. In addition, the iterator returned by entrySet (). iterator () must also implement its remove () method.

 

HashMap-like

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

HashMap is implemented based on the Map interface of the hash table. This class is a commonly used ing. This implementation provides all the optional ing operations defined by the Map interface, allowing the use of null keys and null values. (Except for non-synchronous and allowed null values, the HashMap class is roughly the same as that of HashTable <expired> ). This class does not guarantee the order of mappings, especially it does not guarantee that the order remains unchanged permanently.

Time and time required to iterate the collection ViewHashMapThe instance capacity (number of buckets) and its size (number of key-value ing relationships) are proportional. Therefore, if iteration performance is important, do not set the initial capacity too high (or set the loading factor too low ).

HashMapThe instance has two parameters that affect its performance: initial capacity (16 by default) and load factor (0.75 by default ).

Note that this implementation is not synchronous.

The iterators returned by the "collection view method" of the same class as the List all fail quickly: After the iterator is created, if the ing is modified from the structure, unlessRemoveMethod. The iterator throws any modification at any time.ConcurrentModificationException.

 

Class LinkedHashMap <K, V>

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

MapInterface hash table and Link List Implementation, with predictable iteration order. This class provides all optionalMapOperation, and the null element is allowed. Because the cost of maintaining the link list is increased, the performance is likely to be betterHashMapSlightly inferior, but this exception:LinkedHashMapThe time required for the collection view iteration of is only proportional to the ing size.HashMapIteration time is likely to be costly because the time required is proportional to its capacity.

The Link List defines the iteration order, which is usually the order in which keys are inserted into the ing (insertion order ). Note: If the key is re-inserted in the ing, the insertion sequence is not affected.

Note that this implementation is not synchronous.

Structure ModificationIt refers to any operation that adds or deletes one or more mappings or affects the iteration order in the hash ing of access-ordered links. In the hash ing of the inserted sequential link, only the values associated with the included key in the ing are modified instead of the structure. In the hash ing of access-ordered links, onlyGetThe query ing is not a structure modification.

Like HashMapIteratorThe iterator returned by the method fails quickly.

Because LinkedHashMap inherits HashMap, its implementation is based on HashMap. By maintaining the connection list, it has the predictable iteration sequence. This class overwrites the get (), clear (), and containsValue () methods in the HashMap class.

Note: get () is returned.nullThe value does not necessarily indicate that this ing does not contain the ing relationship of the key; it is also possible that this ing explicitly maps the keynull. AvailablecontainsKey()Operation to distinguish between the two cases.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.