Java源码解析TreeMap简介
treemap是常用的排序树,本文主要介绍treemap中,类的注释中对treemap的介绍。代码如下。
/** * a red-black tree based {@link navigablemap} implementation. * the map is sorted according to the {@linkplain comparable natural * ordering} of its keys, or by a {@link comparator} provided at map * creation time, depending on which constructor is used. * <p>this implementation provides guaranteed log(n) time cost for the * {@code containskey}, {@code get}, {@code put} and {@code remove} * operations. algorithms are adaptations of those in cormen, leiserson, and * rivest's <em>introduction to algorithms</em>. * <p>note that the ordering maintained by a tree map, like any sorted map, and * whether or not an explicit comparator is provided, must be <em>consistent * with {@code equals}</em> if this sorted map is to correctly implement the * {@code map} interface. (see {@code comparable} or {@code comparator} for a * precise definition of <em>consistent with equals</em>.) this is so because * the {@code map} interface is defined in terms of the {@code equals} * operation, but a sorted map performs all key comparisons using its {@code * compareto} (or {@code compare}) method, so two keys that are deemed equal by * this method are, from the standpoint of the sorted map, equal. the behavior * of a sorted map <em>is</em> well-defined even if its ordering is * inconsistent with {@code equals}; it just fails to obey the general contract * of the {@code map} interface. * <p><strong>note that this implementation is not synchronized.</strong> * if multiple threads access a map concurrently, and at least one of the * threads modifies the map structurally, it <em>must</em> be synchronized * externally. (a structural modification is any operation that adds or * deletes one or more mappings; merely changing the value associated * with an existing key is not a structural modification.) this is * typically accomplished by synchronizing on some object that naturally * encapsulates the map. * if no such object exists, the map should be "wrapped" using the * {@link collections#synchronizedsortedmap collections.synchronizedsortedmap} * method. this is best done at creation time, to prevent accidental * unsynchronized access to the map: <pre> * sortedmap m = collections.synchronizedsortedmap(new treemap(...));</pre> * <p>the iterators returned by the {@code iterator} method of the collections * returned by all of this class's "collection view methods" are * <em>fail-fast</em>: if the map is structurally modified at any time after * the iterator is created, in any way except through the iterator's own * {@code remove} method, the iterator will throw a {@link * concurrentmodificationexception}. thus, in the face of concurrent * modification, the iterator fails quickly and cleanly, rather than risking * arbitrary, non-deterministic behavior at an undetermined time in the future. * <p>note that the fail-fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification. fail-fast iterators * throw {@code concurrentmodificationexception} on a best-effort basis. * therefore, it would be wrong to write a program that depended on this * exception for its correctness: <em>the fail-fast behavior of iterators * should be used only to detect bugs.</em> * <p>all {@code map.entry} pairs returned by methods in this class * and its views represent snapshots of mappings at the time they were * produced. they do <strong>not</strong> support the {@code entry.setvalue} * method. (note however that it is possible to change mappings in the * associated map using {@code put}.) * <p>this class is a member of the * <a href="{@docroot}/../technotes/guides/collections/index.html" rel="external nofollow" > * java collections framework</a>. * @param <k> the type of keys maintained by this map * @param <v> the type of mapped values * @author josh bloch and doug lea * @see map * @see hashmap * @see hashtable * @see comparable * @see comparator * @see collection * @since 1.2 **/
这是一个基于红黑树的可导航的实现。这个map基于key的可比较的自然顺序,或者基于在map创建时提供的comparator的顺序来存储元素。
这个实现提供可保证的log(n)的时间复杂度来完成containskey,get,put和remove操作。
需要注意到这一点,不管是否显式提供了排序器,如果这个排序map想要正确实现map接口,tree map维护的顺序必须和equals保持一致,就像任何排序map那样。之所以会这样,是因为map接口是根据equals操作来定义的,但是排序map进行所有key的比较时使用的是他们的compareto方法,所以,从排序map的观点来看,被这个方法认为相等的两个key,才是相等的。尽管如果它的顺序和equals不一致,排序map的行为也是正常的,它只是没有遵守map接口的通常约定。
请注意这个实现是非同步的。如果多个线程并发访问一个treemap,并且至少有一个线程修改结构,必须进行外部同步。这个通常是通过在包含这个map的对象上进行同步来实现的。如果没有这样的对象,那么这个map需要用collections.synchronizedsortedmap方法包装一下。最好是在创建map时就这样做,以防止意外非同步访问这个map。代码如下sortedmap m = collections.synchronizedsortedmap(new treemap(...));
所有这个类的集合视角方法返回的集合的iterator方法返回的迭代器都是fast-fail的:如果迭代器创建后的任何时间map发生了结构性改变,除了通过迭代器的删除方法外,迭代器都会抛出同步修改异常。于是,面对同步修改时,迭代器会迅速干净的失败,而不是冒着在未来的不确定的时间发生不一致或无法确定的行为的风险。
这个类和它的视图的方法返回的map.entry对代表了他们被创建时的快照。他们不支持entry.setvalue方法。
这个类是java集合框架的一个成员。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
上一篇: SSH原理及两种登录方法图文详解
推荐阅读
-
Java源码解析阻塞队列ArrayBlockingQueue功能简介
-
Java源码解析ConcurrentHashMap的初始化
-
Java源码解析TreeMap简介
-
Java源码解析之可重入锁ReentrantLock
-
Java源码解析阻塞队列ArrayBlockingQueue常用方法
-
Java源码解析CopyOnWriteArrayList的讲解
-
Java源码解析HashMap的tableSizeFor函数
-
Java源码解析HashMap的keySet()方法
-
Java源码解析阻塞队列ArrayBlockingQueue介绍
-
java源码解析之String类的compareTo(String otherString)方法