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

在 TreeSet 上使用迭代器

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

問題描述

情況:我有一個自定義對象的 TreeSet,并且我還使用了一個自定義比較器.我創(chuàng)建了一個迭代器以在此 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 循環(huán)中向 TreeSet 添加一個新元素,那么該新元素是否會立即排序.換句話說,如果我在 while 循環(huán)中添加一個新元素并且它小于我當(dāng)前在 c 中保存的元素,那么在下一次迭代中,我會在 c 中獲得與上一次迭代中相同的元素嗎?(因為經(jīng)過排序后,新添加的元素會占據(jù)當(dāng)前元素之前的某個位置).

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).

推薦答案

如果您在迭代期間添加一個元素,您的下一次迭代器調(diào)用可能會拋出 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.

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

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 的建議創(chuàng)建一個單獨的集合,以便在迭代后與 ts 合并.

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

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

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
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)
主站蜘蛛池模板: 午夜精品一区二区三区在线视 | 成人免费视频7777777 | 日本黄色激情视频 | 麻豆成人在线视频 | 美女久久久久久久久 | 在线成人 | 色av一区二区三区 | 国产色| 97国产精品视频人人做人人爱 | 亚洲天堂中文字幕 | 国产精品激情 | 国偷自产av一区二区三区 | 一级特黄在线 | 中文成人在线 | 欧美亚洲第一区 | 午夜精品久久久 | 久久久久国产精品一区 | 日本一区视频在线观看 | 亚洲一区二区三区在线观看免费 | 精品影院 | 中文字幕 欧美 日韩 | 久久精品国产一区二区电影 | 欧美 中文字幕 | 欧美亚洲视频在线观看 | 福利电影在线 | 国产高清在线 | 成人在线精品视频 | 天天综合网天天综合 | 日韩精品一区二区三区在线观看 | 欧洲成人午夜免费大片 | 欧美一区免费 | 精品久久久久久久久久久久 | 日日人人 | 毛片免费在线观看 | 国产无人区一区二区三区 | 毛片网站免费观看 | 91视频官网 | 91国产视频在线观看 | 国产做爰| 免费h视频 | 91精品在线观看入口 |