从HashMap中查找对象键 [英] Find a object key from a HashMap

查看:337
本文介绍了从HashMap中查找对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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中查找对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆