如何创建一个方法来获取哈希图中值(字符串)的键

发布于 2024-10-18 02:24:17 字数 306 浏览 1 评论 0原文

我有一个作业,这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花海 2024-10-25 02:24:17

但实际上你可以有多个带有“今天”的键。所以我用键返回 List.toString

private static HashMap<String, HashSet<String>> hMap = new HashMap<String, HashSet<String>>();

public static String getKey(Map<String, HashSet<String>> map, String value) {
    List<String> returnKey = new ArrayList<String>();

    for (String s : map.keySet()) {
        if (map.get(s).contains(value)) {
            returnKey.add(s);
        }
    }

    return returnKey.toString();

}

public static void main(String[] args) {
    // put sth to hMap
    System.out.println(getKey(hMap, "today"));
}

But actually you can have more than one keys which has "today". So I return List.toString with keys

private static HashMap<String, HashSet<String>> hMap = new HashMap<String, HashSet<String>>();

public static String getKey(Map<String, HashSet<String>> map, String value) {
    List<String> returnKey = new ArrayList<String>();

    for (String s : map.keySet()) {
        if (map.get(s).contains(value)) {
            returnKey.add(s);
        }
    }

    return returnKey.toString();

}

public static void main(String[] args) {
    // put sth to hMap
    System.out.println(getKey(hMap, "today"));
}
满身野味 2024-10-25 02:24:17

你真正想要的是 Google 的 Guava BiMap

bimap(或“双向映射”)是一种保留其值及其键的唯一性的映射。此约束使 bimap 能够支持“反向视图”,这是另一个 bimap,包含与此 bimap 相同的条目,但具有相反的键和值。

如果您不能使用它,那么您将不得不循环遍历地图条目项来寻找速度不太快的匹配项。

What you really want is Google's Guava BiMap.

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

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.

神仙妹妹 2024-10-25 02:24:17

尽可能通用,只使用Map

public static <K,V> K getKey(Map<K,V> map,V val){
    for(Map.Entry<K,V> entry:map.entrySet()){
        if(entry.getValue().equals(val)){
            return entry.getKey();
        }
    }
    return null;
}

这只会返回第一个匹配的值的键。如果您想返回特定值的所有键,则可以修改此方法以将它们的键累积到 List 中并返回该值。

(不要对快速检索抱有希望......)

As generically as possible with nothing but a Map.

public static <K,V> K getKey(Map<K,V> map,V val){
    for(Map.Entry<K,V> entry:map.entrySet()){
        if(entry.getValue().equals(val)){
            return entry.getKey();
        }
    }
    return null;
}

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...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文