久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 TreeSet 上使用迭代器

Using iterator on a TreeSet(在 TreeSet 上使用迭代器)
本文介紹了在 TreeSet 上使用迭代器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

情況:我有一個自定義對象的 TreeSet,并且我還使用了一個自定義比較器.我創建了一個迭代器以在此 TreeSet 上使用.

SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet.

TreeSet<Custom> ts=new TreeSet<Custom>();
Iterator<Custom> itr=ts.iterator();
while(itr.hasNext()){
    Custom c=itr.next();
    //Code to add a new element to the TreeSet ts
}

問題:好吧,我想知道,如果我在 while 循環中向 TreeSet 添加一個新元素,那么該新元素是否會立即排序.換句話說,如果我在 while 循環中添加一個新元素并且它小于我當前在 c 中保存的元素,那么在下一次迭代中,我會在 c 中獲得與上一次迭代中相同的元素嗎?(因為經過排序后,新添加的元素會占據當前元素之前的某個位置).

QUESTION: Well I want to know that if I add a new element to the TreeSet within the while loop, then will that new element get sorted immediately. In other words, if I add a new element within the while loop and it is less than the one which I am currently holding in c, then in the next iteration will I be getting the same element in c as in the last iteration?(since after sorting, the newly added element will occupy a place somewhere before the current element).

推薦答案

如果您在迭代期間添加一個元素,您的下一次迭代器調用可能會拋出 ConcurrentModificationException.請參閱 TreeSet 文檔中的快速失敗行為.

If you add an element during your iteration, your next iterator call will likely throw a ConcurrentModificationException. See the fail-fast behavior in TreeSet docs.

要迭代和添加元素,您可以先復制到另一個集合:

To iterate and add elements, you could copy first to another set:

TreeSet<Custom> ts = ...
TreeSet<Custom> tsWithExtra = new TreeSet(ts);

for (Custom c : ts) {
  // possibly add to tsWithExtra
}

// continue, using tsWithExtra

或者按照 Colin 的建議創建一個單獨的集合,以便在迭代后與 ts 合并.

or create a separate collection to be merged with ts after iteration, as Colin suggests.

這篇關于在 TreeSet 上使用迭代器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 天堂一区在线 | 亚洲精品小视频在线观看 | 国产黄色小视频在线观看 | 欧美精品一二三 | 亚洲一区有码 | 蜜桃臀av一区二区三区 | 国产农村妇女毛片精品久久麻豆 | 国产精品成人一区二区三区 | 毛片一区二区 | 91麻豆产精品久久久久久夏晴子 | 亚洲免费观看视频网站 | 亚洲精品视频在线播放 | 福利片一区二区 | 中文字幕日韩av | 中文字幕亚洲精品 | www.国产精 | 亚洲国产一区二区三区在线观看 | 久久99精品久久久久久国产越南 | 夜夜操av| 草草草久久久 | 日韩国产一区二区 | 日韩av看片 | 国产偷自视频区视频 | 日韩午夜在线观看 | av特级毛片| 欧美精品一二三 | 久久宗合色 | 伊人久久综合影院 | 精品综合在线 | 91精品国产色综合久久 | 成人免费淫片aa视频免费 | 午夜影院在线观看视频 | 欧美一区二区三区在线观看 | 青娱乐av| 亚洲精品一区二区在线 | 午夜亚洲| 99久久久国产精品 | 欧美亚洲综合久久 | 新超碰97| 理伦毛片 | 天天综合天天 |