如何创建一个方法来获取哈希图中值(字符串)的键
我有一个作业,这是我的 HashMap 初始化。我只能使用标准 JAVA API。
private static HashMap<String, HashSet<String>> hMap = new HashMap<String, HashSet<String>>();
是否可以获取指定值的键(例如 getkey(hMap,"today") ),并且它会返回今天所在的键。
还有没有办法获取 HashSet 中键的最后一个值?
任何帮助表示感谢!
I have an assignment, This is my HashMap initialization.. I can only use standard JAVA API.
private static HashMap<String, HashSet<String>> hMap = new HashMap<String, HashSet<String>>();
Is it possible to get the Key of a specified value say getkey(hMap,"today") and it would return the key where today is located..
Aslo is there a way to get the last value of a Key in my HashSet?
Any help is appreciated thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
但实际上你可以有多个带有“今天”的键。所以我用键返回 List.toString
But actually you can have more than one keys which has "today". So I return List.toString with keys
你真正想要的是 Google 的 Guava BiMap 。
如果您不能使用它,那么您将不得不循环遍历地图条目项来寻找速度不太快的匹配项。
What you really want is Google's Guava BiMap.
If you can't use that, then you're going to have to loop through the maps entry items looking for a match which isn't overly speedy.
尽可能通用,只使用
Map
。这只会返回第一个匹配的值的键。如果您想返回特定值的所有键,则可以修改此方法以将它们的键累积到
List
中并返回该值。(不要对快速检索抱有希望......)
As generically as possible with nothing but a
Map
.This will only return the key of the first value that matches. If you want to return all the keys for a specific value, then you can modify this method to accumulate they keys into a
List
and return that instead.(Don't get your hopes up for a speedy retrieval...)