Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a object key from a HashMap

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;

I want to get the reference of the "file" object in the keys of the map.

like image 260
Amit Avatar asked Feb 24 '23 20:02

Amit


1 Answers

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.

like image 100
krtek Avatar answered Feb 27 '23 12:02

krtek