从HashMap中查找对象键

编程入门 行业动态 更新时间:2024-10-19 07:26:57
本文介绍了从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中查找对象键

本文发布于:2023-11-07 06:05:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   HashMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!