Map of Java Collection framework and map of java Collection framework

Source: Internet
Author: User
Tags rehash

Map of Java Collection framework and map of java Collection framework

 

HashtableHashtable instances have two parameters that affect their performance: initial capacity and load factor. Capacity is the number of buckets in the hash table, and the initial capacity is the capacity when the hash table is created. Note: The hash table is in the open state: in the case of a "hash Conflict", multiple entries are stored in a single bucket, and these entries must be searched in order. The load factor is a scale that can reach the full capacity of a hash table before its capacity is automatically increased. The initial capacity and loading Factor parameters are only prompts for this implementation. The specific details about when and whether to call the rehash method depend on this implementation. Generally, the default load factor (. 75) seeks a compromise between time and space costs. Although the loading factor is too high, it reduces the space overhead, but it also increases the time for searching for a certain entry (which is reflected in most Hashtable operations, including get and put operations ). The initial capacity mainly controls the balance between the space consumption and the time consumption required to perform the rehash operation. If the initial capacity is greater than the maximum number of entries in Hashtable divided by the load factor, the rehash operation will never occur. However, setting the initial capacity too high may result in a waste of space. Hashtable is synchronous. In general, HashMap is used instead of the old Hashtbale. Even if synchronization is required, it also uses the synchronized HashMap or ConcurrentHashMap. PropertiesThis class is inherited from Hashtable and is mainly used to read configuration files in the Java key-value format, such as XML files and properties files. In the comments of this class, we recommend that you use the setProperties method to Insert key values, instead of put and putAll, because these two methods allow callers to Insert key values that are not String items. HashMapImplementation of the Map interface based on the hash table. This implementation provides all optional ing operations and allows the use of null values and null keys. (The HashMap class is roughly the same as that of Hashtable except for non-synchronous and allowed null .) This class does not guarantee the order of mappings, especially it does not guarantee that the order remains unchanged. The HashMap instance has two parameters that affect its performance: initial capacity and load factor. Capacity is the number of buckets in the hash table. The initial capacity is only the capacity of the hash table at creation. A load factor is a scale in which a hash table can reach full capacity before its capacity increases automatically. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, You need to rehash the hash table (that is, rebuild the internal data structure ), in this way, the hash table will have approximately two times the number of buckets. Generally, the default load factor (. 75) seeks a compromise between time and space costs. Although the loading factor is too high, it reduces the space overhead, but it also increases the query cost (this is reflected in most HashMap operations, including get and put operations ). When setting the initial capacity, you should take into account the number of entries and their loading factors required in the ing to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, the rehash operation is not performed. This class is not thread-safe. TreeMapThe figure shows that the TreeMap class not only implements the Map interface, but also implements the Map interface sub-interface java. util. SortedMap. It is implemented based on the NavigableMap of the Red-Black tree. The ing is sorted by the natural sequence of its keys, or by the Comparator provided when the ing is created, depending on the construction method used. In terms of adding, deleting, and locating ing relationships, the TreeMap class has lower performance than the HashMap class, but the ing relationships have a certain order. If you do not need an ordered set, we recommend that you use the HashMap class. If you need to traverse the output in sequence, we recommend that you use the TreeMap class. In this case, you can first use the Map set implemented by the HashMap class, when sequential output is required, use an existing HashMap instance to create a TreeMap instance with identical mappings. LinkedHashMapThis class is implemented by the hash table and linked list of the Map interface. This class maintains a list of dual links running on all entries. The Link List defines the iteration order. This iteration order usually refers to the order in which the key is inserted into the ing (the insertion order), that is, it retains the insertion order, the output sequence is the insert sequence of input. This implementation provides all optional ing operations and allows the use of null values and null keys. This class does not guarantee the order of mappings, especially it does not guarantee that the order remains unchanged. The sequence of elements in the linked list can be divided into: the linked list in the insert order, and the linked list in the access order (call the get method. By default, it is sorted by insert order. If you specify to sort by access order, after the get method is called, The accessed elements will be moved to the end of the linked list, continuous access can form a linked list sorted by access order. You can override the removeEldestEntry method to return the true value, specifying that the oldest element is removed when an element is inserted. Note that this implementation is not synchronous. WeakHashMapHash table-Based Map implemented with weak keys. In WeakHashMap, when a key is no longer used normally, its entries are automatically removed. More precisely, for a given key, the existence of its ing does not prevent the garbage collector from discarding the key, which makes the key terminable and terminated, then it is recycled. When a key is discarded, its entries are effectively removed from the ing. Therefore, the behavior of this class is different from that of other Map implementations. ConcurrentHashMapThis class is a thread-safe Map implementation. Similar to Hashtable, but Hashtable's synchronized is for the entire Hash table, that is, each time the entire table is locked, the thread is exclusive, and ConcurrentHashMap allows multiple modification operations to be performed concurrently, the key lies in the use of the lock separation technology. It uses multiple locks to control the modification of different parts of the hash table. ConcurrentHashMap uses segments to represent these different parts. Each Segment is actually a small hash table and they have their own locks. As long as multiple modifications occur on different segments, they can be performed concurrently. Hashtable uses synchronization operations for get, put, and remove. Its synchronization level is synced to Hashtable. That is to say, if a thread is traversing the set, other threads will not be able to use this set for the time being. This will undoubtedly easily affect performance and throughput, thus forming a single point of failure. ConcurrentHashMap is different. It only uses the synchronization operation for the put and remove operations, and the get operation does not affect the operation. This class does not allow null as a key or value. ConcurrentSkipListMapThis class is a scalable concurrent ConcurrentNavigableMap implementation. Ing can be sorted by the natural order of keys, or by the Comparator provided when the ing is created, depending on the construction method used. This kind of concurrent variation of SkipLists provides the expected average log (n) time overhead for the containsKey, get, put, remove, and their variants. Multiple Threads can safely and concurrently perform insert, remove, update, and access operations. The iterator is weak and consistent, and the returned elements will reflect the ing status at the time when the iterator is created or after it is created. They do not throw ConcurrentModificationException and can concurrently process other operations. The ascending key sorting view and its iterator are faster than the descending key sorting view and its iterator. Compared with ConcurrentHashMap, not only keys are ordered, but also ConcurrentSkipListMap supports higher concurrency. The access time of ConcurrentSkipListMap is log (N), which is almost independent of the number of threads. That is to say, with a certain amount of data, the more concurrent threads, the more ConcurrentSkipListMap can reflect its advantages. Note: when calling the size of ConcurrentSkipListMap, since multiple threads can operate on the ing table at the same time, the ing table must traverse the entire linked list to return the number of elements, this operation is an O (log (n) operation. This class does not allow null keys or values. Reprinted Please note:Http://www.cnblogs.com/LeslieXia/p/5788816.html

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.