Java源码解析TreeMap简介

 更新时间:2019年01月08日 10:05:19   作者:李灿辉  
今天小编就为大家分享一篇关于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集合框架的一个成员。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • Spring探秘之如何妙用BeanPostProcessor

    Spring探秘之如何妙用BeanPostProcessor

    BeanPostProcessor也称为Bean后置处理器,它是Spring中定义的接口,在Spring容器的创建过程中会回调BeanPostProcessor中定义的两个方法,这篇文章主要给大家介绍了关于Spring探秘之如何妙用BeanPostProcessor的相关资料,需要的朋友可以参考下
    2022-01-01
  • java理论基础Stream元素的匹配与查找

    java理论基础Stream元素的匹配与查找

    这篇文章主要为大家介绍了java理论基础Stream元素的匹配与查找方法的示例说明解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • Java中的@PreAuthorize注解使用详解

    Java中的@PreAuthorize注解使用详解

    这篇文章主要介绍了Java中的@PreAuthorize注解使用详解,@PreAuthorize注解会在方法执行前进行权限验证,支持Spring EL表达式,它是基于方法注解的权限解决方案,需要的朋友可以参考下
    2023-10-10
  • java stringbuffer的用法示例

    java stringbuffer的用法示例

    这篇文章主要介绍了java stringbuffer的用法示例,字符串缓冲区,是一个容器(当返回到的是String时而且长度不确定,数据类型不确定时就可以用StringBuffer)其实底层还是数组,只是被封装了,对外提供了方法,初始容量为16个字符
    2014-01-01
  • RocketMQ ConsumeQueue与IndexFile实时更新机制源码解析

    RocketMQ ConsumeQueue与IndexFile实时更新机制源码解析

    这篇文章主要为大家介绍了RocketMQ ConsumeQueue与IndexFile实时更新机制源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • 在Java中实现线程安全的单例模式的常见方式

    在Java中实现线程安全的单例模式的常见方式

    单例模式是一种常用的软件设计模式,它确保一个类只有一个实例,并提供一个全局访问点,在多线程环境下,确保单例模式的线程安全性是非常重要的,因为多个线程可能会同时尝试创建实例,导致实例不唯一的问题,本文介绍了在Java中实现线程安全的单例模式有几种常见的方式
    2024-09-09
  • Eclipse查看开发包jar里源代码的方法

    Eclipse查看开发包jar里源代码的方法

    这篇文章主要介绍了Eclipse查看开发包jar里源代码的方法的相关资料,需要的朋友可以参考下
    2017-07-07
  • Java面试常考之ConcurrentHashMap多线程扩容机制详解

    Java面试常考之ConcurrentHashMap多线程扩容机制详解

    几乎所有的后端技术面试官都要在 ConcurrentHashMap 技术的使用和原理方面对小伙伴们进行刁难,本文主要来和大家聊聊ConcurrentHashMap多线程的扩容机制,希望对大家有所帮助
    2023-05-05
  • Java注解处理器学习之编译时处理的注解详析

    Java注解处理器学习之编译时处理的注解详析

    编译时注解相信对每一个java开发者来说都不陌生,下面这篇文章主要给大家介绍了关于Java注解处理器学习之编译时处理的注解的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧
    2018-05-05
  • 使用maven一步一步构建spring mvc项目(图文详解)

    使用maven一步一步构建spring mvc项目(图文详解)

    这篇文章主要介绍了详解使用maven一步一步构建spring mvc项目,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09

最新评论