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

如果 Set 包含具有某些字符串值的對(duì)象,如何檢查

How to check in java if Set contains object with some string value?(如果 Set 包含具有某些字符串值的對(duì)象,如何檢查 java?)
本文介紹了如果 Set 包含具有某些字符串值的對(duì)象,如何檢查 java?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一組對(duì)象.每個(gè)對(duì)象都有 String 值.

I have Set of objects. Each object has String value.

我需要選擇所有 this 值等于direction"的對(duì)象.

I need to select all objects that have this value equal to "direction".

是否可以不迭代集合?

推薦答案

一般來(lái)說(shuō),沒(méi)有.您需要遍歷集合并檢查每個(gè)對(duì)象以查看屬性是否等于您正在搜索的值.這是一個(gè) O(n) 操作.

In general, no. You need to iterate over the set and check each object to see if the property is equal to the value you are searching for. This is an O(n) operation.

在一種情況下,您無(wú)需迭代即可完成.如果您的對(duì)象的 equals 方法是根據(jù)該 String 屬性的相等性定義的,并且如果 hashCode 方法也正確實(shí)現(xiàn),那么您可以使用 hashSet.containsO(1) 時(shí)間內(nèi)找到具有正確值的對(duì)象,而無(wú)需遍歷集合.

There is one situation in which you could do it without iterating. If your object's equals method is defined in terms of equality of that String property, and if the hashCode method is also implemented correctly, then you can use the hashSet.contains to find an object with the correct value in O(1) time without requiring iterating over the set.

正如我所提到的,這是一個(gè)非常具體的用例,而不是通用解決方案.如果字符串是某種唯一標(biāo)識(shí)符,它可能會(huì)很有用,但它不適用于您的特定用例.

As I mentioned, this is a very specific use case and not a general solution. It might be useful if the string was some sort of unique identifier, but it won't work for your specific use case.

您可能還想考慮其他更適合您的用例的集合.例如,如果您使用 Guava,那么您可以考慮使用 多地圖.

You might also want to consider other collections that would be better suited to your use case. You could for example if you are using Guava then you could consider using a Multimap.

相關(guān)

  • HashMap在同一個(gè)鍵下有多個(gè)值

這篇關(guān)于如果 Set 包含具有某些字符串值的對(duì)象,如何檢查 java?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來(lái)自服務(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 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 欧美成人精品欧美一级 | 91人人看| 日本黄色免费片 | 成人免费视频 | 国产欧美在线 | 成人啊啊啊| 二区在线观看 | 欧美精品一区二区三区四区 在线 | 青青久久久 | 国产精品www | 天天干亚洲 | 国产乱码精品一区二区三区中文 | 五月天婷婷丁香 | 成人在线视频一区 | 免费激情网站 | 91视频进入 | av一区在线 | 中文字幕av一区 | 精品国产欧美一区二区 | 午夜精品久久久久久不卡欧美一级 | 伊人激情网 | 亚洲电影一区 | hsck成人网| 一区二区三区久久 | 成人在线免费电影 | 精品国产乱码久久久久久牛牛 | 亚洲综合电影 | www.操.com| 欧美日一区二区 | a精品视频| 国产在线观看一区二区 | www.天天操.com| 夜夜夜久久 | 妞干网视频 | 精品久| 欧美精品一区二区三区在线播放 | 91一区二区 | 99视频免费在线 | 免费日韩av | 亚洲一区二区 | 在线不卡一区 |