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

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

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

問題描述

我有一組對象.每個對象都有 String 值.

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

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

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

是否可以不迭代集合?

推薦答案

一般來說,沒有.您需要遍歷集合并檢查每個對象以查看屬性是否等于您正在搜索的值.這是一個 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.

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

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.

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

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.

相關

  • HashMap在同一個鍵下有多個值

這篇關于如果 Set 包含具有某些字符串值的對象,如何檢查 java?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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在线 | 综合二区 | 欧美一级精品片在线看 | 午夜一区 | 99久久免费精品国产男女高不卡 | 成人在线视频免费观看 | 精品一区在线看 | 欧美精品久久久久久久久老牛影院 | 欧美精品一区三区 | 中文字幕在线观看一区二区 | 欧美男人天堂 | 国产精品久久久一区二区三区 | 国产精品区一区二区三 | 欧美成人aaa级毛片在线视频 | 免费一区 | 欧美激情久久久 | 99re视频在线免费观看 | 国产一区欧美 | 久久99精品国产麻豆婷婷 | 日本中文字幕在线观看 | 亚洲最色网站 | 综合色在线 | 亚洲精品麻豆 | 久久亚洲欧美日韩精品专区 | 亚洲成人精品在线观看 | 农夫在线精品视频免费观看 | 精品国产色| 久热精品在线播放 | 国产视频h | 久久久精品 | 黄色国产 | 国产激情网站 | 欧美一级片在线播放 | 日韩av免费在线电影 | 久久久久国产精品一区二区 |