我有一个HashMap有键作为我自己的对象和键作为ArrayList的String。是否有一种方法从地图中获取等于另一个对象的键对象,而不迭代地图。请注意,我的对象已经实现了equals&哈希码。它只使用类的2属性进行比较。我试图在地图的键中找到的另一个对象具有等于的那些2属性,但是其他属性在地图的键中可能不同。
I have a HashMap having key as my own object and key as ArrayList of String. Is there a way to get the key object from the map which is equal to another object without iterating the map. Please note that my object has implemented equals & hashcode. And it only uses 2 attribute of the class to compare. The another object which I am trying to find in the keys of the map has those 2 attribute equal but the other attributes may be different in the key of the map.
//The actual map private HashMap<FileDetail, ArrayList<String>> map = new HashMap<FileDetail, ArrayList<String>>(); //object to search in above map without iteration. FileDetail file = some object;我想获取地图键中文件对象的引用。 p>
I want to get the reference of the "file" object in the keys of the map.
推荐答案不,你不能这样做。 HashMap 应该以其他方式工作:你有键,你正在寻找对象。
No you can't do that. HashMap are supposed to work the other way : you have the key, you're looking for the object.
如果你有一个对象,你想找到的关键,可能是你的逻辑错误,你在一个错误的方向来解决你的问题。
If you have an object and you want to find the key, there's probably something wrong in your logic and your looking in a wrong direction to solve your problem.
更多推荐
从HashMap中查找对象键
发布评论