标签:des android style blog class c
/** * A {@code Map} is a data structure consisting of a set of keys and values * in which each key is mapped to a single value. The class of the objects * used as keys is declared when the {@code Map} is declared, as is the * class of the corresponding values. * <p> * A {@code Map} provides helper methods to iterate through all of the * keys contained in it, as well as various methods to access and update * the key/value pairs. */
一个map是由一组键和值组成的数据结构 ,其中每个键映射到一个单一的值。当Map被声明后,values的值将被作为key 的关联值被声明
/** * HashMap is an implementation of {@link Map}. All optional operations are supported. * * <p>All elements are permitted as keys or values, including null. * * <p>Note that the iteration order for HashMap is non-deterministic. If you want * deterministic iteration, use {@link LinkedHashMap}. * * <p>Note: the implementation of {@code HashMap} is not synchronized. * If one thread of several threads accessing an instance modifies the map * structurally, access to the map needs to be synchronized. A structural * modification is an operation that adds or removes an entry. Changes in * the value of an entry are not structural changes. * * <p>The {@code Iterator} created by calling the {@code iterator} method * may throw a {@code ConcurrentModificationException} if the map is structurally * changed while an iterator is used to iterate over the elements. Only the * {@code remove} method that is provided by the iterator allows for removal of * elements during iteration. It is not possible to guarantee that this * mechanism works in all cases of unsynchronized concurrent modification. It * should only be used for debugging purposes. * * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */
HashMap继承了map,实现了map的所有方法。key和value允许使用全部的元素,包括null
/** * Hashtable is a synchronized implementation of {@link Map}. All optional operations are supported. * * <p>Neither keys nor values can be null. (Use {@code HashMap} or {@code LinkedHashMap} if you * need null keys or values.) * * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values * @see HashMap */
Hashtable 同步继承map,支持map的全部操作。
/** * A {@link java.util.Map} providing additional atomic * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods. * * <p>Memory consistency effects: As with other concurrent * collections, actions in a thread prior to placing an object into a * {@code ConcurrentMap} as a key or value * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> * actions subsequent to the access or removal of that object from * the {@code ConcurrentMap} in another thread. * * @since 1.5 * @author Doug Lea * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */
一个map的附加方法,提供putIfAbsent和remove(移除)、replace(替换)方法。
/** * If the specified key is not already associated * with a value, associate it with the given value. * This is equivalent to * <pre> * if (!map.containsKey(key)) * return map.put(key, value); * else * return map.get(key);</pre> * except that the action is performed atomically. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * <tt>null</tt> if there was no mapping for the key. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map * */ V putIfAbsent(K key, V value);
这个意思简单,直接看代码:
if (!map.containsKey(key)) return map.put(key, value); else return map.get(key);
如果map不包含key那么就添加这个key、values否则返回这个key的values值。不允许key为null
/** * A hash table supporting full concurrency of retrievals and * adjustable expected concurrency for updates. This class obeys the * same functional specification as {@link java.util.Hashtable}, and * includes versions of methods corresponding to each method of * <tt>Hashtable</tt>. However, even though all operations are * thread-safe, retrieval operations do <em>not</em> entail locking, * and there is <em>not</em> any support for locking the entire table * in a way that prevents all access. This class is fully * interoperable with <tt>Hashtable</tt> in programs that rely on its * thread safety but not on its synchronization details. * * <p> Retrieval operations (including <tt>get</tt>) generally do not * block, so may overlap with update operations (including * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results * of the most recently <em>completed</em> update operations holding * upon their onset. For aggregate operations such as <tt>putAll</tt> * and <tt>clear</tt>, concurrent retrievals may reflect insertion or * removal of only some entries. Similarly, Iterators and * Enumerations return elements reflecting the state of the hash table * at some point at or since the creation of the iterator/enumeration. * They do <em>not</em> throw {@link ConcurrentModificationException}. * However, iterators are designed to be used by only one thread at a time. * * <p> The allowed concurrency among update operations is guided by * the optional <tt>concurrencyLevel</tt> constructor argument * (default <tt>16</tt>), which is used as a hint for internal sizing. The * table is internally partitioned to try to permit the indicated * number of concurrent updates without contention. Because placement * in hash tables is essentially random, the actual concurrency will * vary. Ideally, you should choose a value to accommodate as many * threads as will ever concurrently modify the table. Using a * significantly higher value than you need can waste space and time, * and a significantly lower value can lead to thread contention. But * overestimates and underestimates within an order of magnitude do * not usually have much noticeable impact. A value of one is * appropriate when it is known that only one thread will modify and * all others will only read. Also, resizing this or any other kind of * hash table is a relatively slow operation, so, when possible, it is * a good idea to provide estimates of expected table sizes in * constructors. * * <p>This class and its views and iterators implement all of the * <em>optional</em> methods of the {@link Map} and {@link Iterator} * interfaces. * * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class * does <em>not</em> allow <tt>null</tt> to be used as a key or value. * * @since 1.5 * @author Doug Lea * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */
一个哈希表,它支持检索的完全并发和更新的所期望可调整并发。这个类遵循相同的功能规范的哈希表,并包括相应的哈希表的每个方法的方法版本。然而,即使所有操作都是线程安全的,检索操作不涉及锁定,并没有锁定整个表中,可以防止所有的接入方式的任何支持. 这个类是完全兼容的Hashtable中依赖于它的线程安全,但不能在其同步的细节计划。
/** * LinkedHashMap is an implementation of {@link Map} that guarantees iteration order. * All optional operations are supported. * * <p>All elements are permitted as keys or values, including null. * * <p>Entries are kept in a doubly-linked list. The iteration order is, by default, the * order in which keys were inserted. Reinserting an already-present key doesn‘t change the * order. If the three argument constructor is used, and {@code accessOrder} is specified as * {@code true}, the iteration will be in the order that entries were accessed. * The access order is affected by {@code put}, {@code get}, and {@code putAll} operations, * but not by operations on the collection views. * * <p>Note: the implementation of {@code LinkedHashMap} is not synchronized. * If one thread of several threads accessing an instance modifies the map * structurally, access to the map needs to be synchronized. For * insertion-ordered instances a structural modification is an operation that * removes or adds an entry. Access-ordered instances also are structurally * modified by {@code put}, {@code get}, and {@code putAll} since these methods * change the order of the entries. Changes in the value of an entry are not structural changes. * * <p>The {@code Iterator} created by calling the {@code iterator} method * may throw a {@code ConcurrentModificationException} if the map is structurally * changed while an iterator is used to iterate over the elements. Only the * {@code remove} method that is provided by the iterator allows for removal of * elements during iteration. It is not possible to guarantee that this * mechanism works in all cases of unsynchronized concurrent modification. It * should only be used for debugging purposes. */
节点都保存在一个双向链表。迭代顺序是,默认情况下,顺序钥匙插入。重新插入一个已经存在键不改变顺序。如果三个参数的构造函数被使用,并且accessOrder 被指定为true ,迭代将在该条目被访问的顺序。
android开发之-数据存储Map、HashMap、Hashtable、concurenthashmap区别,布布扣,bubuko.com
android开发之-数据存储Map、HashMap、Hashtable、concurenthashmap区别
标签:des android style blog class c
原文地址:http://www.cnblogs.com/dava/p/3738721.html