最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java源碼解析TreeMap簡(jiǎn)介

 更新時(shí)間:2019年01月08日 10:05:19   作者:李燦輝  
今天小編就為大家分享一篇關(guān)于Java源碼解析TreeMap簡(jiǎn)介,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

TreeMap是常用的排序樹,本文主要介紹TreeMap中,類的注釋中對(duì)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
 **/

這是一個(gè)基于紅黑樹的可導(dǎo)航的實(shí)現(xiàn)。這個(gè)map基于key的可比較的自然順序,或者基于在map創(chuàng)建時(shí)提供的Comparator的順序來存儲(chǔ)元素。

這個(gè)實(shí)現(xiàn)提供可保證的log(n)的時(shí)間復(fù)雜度來完成containsKey,get,put和remove操作。

需要注意到這一點(diǎn),不管是否顯式提供了排序器,如果這個(gè)排序map想要正確實(shí)現(xiàn)Map接口,tree map維護(hù)的順序必須和equals保持一致,就像任何排序map那樣。之所以會(huì)這樣,是因?yàn)镸ap接口是根據(jù)equals操作來定義的,但是排序map進(jìn)行所有key的比較時(shí)使用的是他們的compareTo方法,所以,從排序map的觀點(diǎn)來看,被這個(gè)方法認(rèn)為相等的兩個(gè)key,才是相等的。盡管如果它的順序和equals不一致,排序map的行為也是正常的,它只是沒有遵守Map接口的通常約定。

請(qǐng)注意這個(gè)實(shí)現(xiàn)是非同步的。如果多個(gè)線程并發(fā)訪問一個(gè)treemap,并且至少有一個(gè)線程修改結(jié)構(gòu),必須進(jìn)行外部同步。這個(gè)通常是通過在包含這個(gè)map的對(duì)象上進(jìn)行同步來實(shí)現(xiàn)的。如果沒有這樣的對(duì)象,那么這個(gè)map需要用Collections.synchronizedSortedMap方法包裝一下。最好是在創(chuàng)建map時(shí)就這樣做,以防止意外非同步訪問這個(gè)map。代碼如下SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));

所有這個(gè)類的集合視角方法返回的集合的iterator方法返回的迭代器都是fast-fail的:如果迭代器創(chuàng)建后的任何時(shí)間map發(fā)生了結(jié)構(gòu)性改變,除了通過迭代器的刪除方法外,迭代器都會(huì)拋出同步修改異常。于是,面對(duì)同步修改時(shí),迭代器會(huì)迅速干凈的失敗,而不是冒著在未來的不確定的時(shí)間發(fā)生不一致或無法確定的行為的風(fēng)險(xiǎn)。

這個(gè)類和它的視圖的方法返回的Map.Entry對(duì)代表了他們被創(chuàng)建時(shí)的快照。他們不支持Entry.setValue方法。

這個(gè)類是Java集合框架的一個(gè)成員。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • Spring探秘之如何妙用BeanPostProcessor

    Spring探秘之如何妙用BeanPostProcessor

    BeanPostProcessor也稱為Bean后置處理器,它是Spring中定義的接口,在Spring容器的創(chuàng)建過程中會(huì)回調(diào)BeanPostProcessor中定義的兩個(gè)方法,這篇文章主要給大家介紹了關(guān)于Spring探秘之如何妙用BeanPostProcessor的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • java理論基礎(chǔ)Stream元素的匹配與查找

    java理論基礎(chǔ)Stream元素的匹配與查找

    這篇文章主要為大家介紹了java理論基礎(chǔ)Stream元素的匹配與查找方法的示例說明解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java中的@PreAuthorize注解使用詳解

    Java中的@PreAuthorize注解使用詳解

    這篇文章主要介紹了Java中的@PreAuthorize注解使用詳解,@PreAuthorize注解會(huì)在方法執(zhí)行前進(jìn)行權(quán)限驗(yàn)證,支持Spring EL表達(dá)式,它是基于方法注解的權(quán)限解決方案,需要的朋友可以參考下
    2023-10-10
  • java stringbuffer的用法示例

    java stringbuffer的用法示例

    這篇文章主要介紹了java stringbuffer的用法示例,字符串緩沖區(qū),是一個(gè)容器(當(dāng)返回到的是String時(shí)而且長(zhǎng)度不確定,數(shù)據(jù)類型不確定時(shí)就可以用StringBuffer)其實(shí)底層還是數(shù)組,只是被封裝了,對(duì)外提供了方法,初始容量為16個(gè)字符
    2014-01-01
  • RocketMQ?ConsumeQueue與IndexFile實(shí)時(shí)更新機(jī)制源碼解析

    RocketMQ?ConsumeQueue與IndexFile實(shí)時(shí)更新機(jī)制源碼解析

    這篇文章主要為大家介紹了RocketMQ?ConsumeQueue與IndexFile實(shí)時(shí)更新機(jī)制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 在Java中實(shí)現(xiàn)線程安全的單例模式的常見方式

    在Java中實(shí)現(xiàn)線程安全的單例模式的常見方式

    單例模式是一種常用的軟件設(shè)計(jì)模式,它確保一個(gè)類只有一個(gè)實(shí)例,并提供一個(gè)全局訪問點(diǎn),在多線程環(huán)境下,確保單例模式的線程安全性是非常重要的,因?yàn)槎鄠€(gè)線程可能會(huì)同時(shí)嘗試創(chuàng)建實(shí)例,導(dǎo)致實(shí)例不唯一的問題,本文介紹了在Java中實(shí)現(xiàn)線程安全的單例模式有幾種常見的方式
    2024-09-09
  • Eclipse查看開發(fā)包jar里源代碼的方法

    Eclipse查看開發(fā)包jar里源代碼的方法

    這篇文章主要介紹了Eclipse查看開發(fā)包jar里源代碼的方法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Java面試??贾瓹oncurrentHashMap多線程擴(kuò)容機(jī)制詳解

    Java面試??贾瓹oncurrentHashMap多線程擴(kuò)容機(jī)制詳解

    幾乎所有的后端技術(shù)面試官都要在?ConcurrentHashMap?技術(shù)的使用和原理方面對(duì)小伙伴們進(jìn)行刁難,本文主要來和大家聊聊ConcurrentHashMap多線程的擴(kuò)容機(jī)制,希望對(duì)大家有所幫助
    2023-05-05
  • Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解詳析

    Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解詳析

    編譯時(shí)注解相信對(duì)每一個(gè)java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧
    2018-05-05
  • 使用maven一步一步構(gòu)建spring mvc項(xiàng)目(圖文詳解)

    使用maven一步一步構(gòu)建spring mvc項(xiàng)目(圖文詳解)

    這篇文章主要介紹了詳解使用maven一步一步構(gòu)建spring mvc項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評(píng)論

靖江市| 彭阳县| 明光市| 景洪市| 木里| 竹山县| 太原市| 舞阳县| 明光市| 新邵县| 兴安盟| 海盐县| 咸宁市| 云南省| 西安市| 方山县| 新巴尔虎右旗| 海林市| 宾川县| 定远县| 抚远县| 连云港市| 溧阳市| 盱眙县| 福清市| 临湘市| 广丰县| 临澧县| 兰考县| 丹东市| 叶城县| 瓦房店市| 敦化市| 西乌珠穆沁旗| 汕头市| 呼玛县| 休宁县| 澜沧| 洛隆县| 毕节市| 扬中市|