HashMap.containsKey() - 如何搜索类?

发布于 2024-11-14 04:01:29 字数 1322 浏览 4 评论 0原文

你好
如果您在 HashMap 中搜索键值对的特定值,您可以编写以下内容:

myHashMap.containsKey(myString);

但是如果键不是字符串,我该如何管理它呢?我有一个如下所示的类:

public class Kategorie implements Comparable {
    private String name;

    public Kategorie()  {
        super();
    }

    public Kategorie(String name)  {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Kategorie))  
           throw new ClassCastException();

        Kategorie k = (Kategorie)o;
        String name = k.getName();
        return this.getName().compareTo(name);

    }
}

在地图中,我保存了这种类型“Kategorie”的键和值。

mapKategorieDEundEN.put(new Kategorie(strName_de), new Kategorie(strName_en));

稍后在代码中,我想检查是否存在带有特定字符串的键。

if (mapKategorieDEundEN.containsKey(searchString))  {

...不起作用,因为密钥不是字符串而是“类别”,这一点很清楚。

然后我尝试了这样的事情:

if (mapKategorieDEundEN.containsKey(new Kategorie(searchString)))  {

...也不起作用。我认为它没有找到任何东西,因为该对象不是“原始”对象,而是一个新对象。

在这种情况下,我可以使用 containsKey 还是必须在 HashMap 上使用循环?

Hello
if you search in an HashMap<String,String> for a specific value of a key-value-pair, you can write the following:

myHashMap.containsKey(myString);

But how can I manage it if the key is not a string? I have a class which looks like this:

public class Kategorie implements Comparable {
    private String name;

    public Kategorie()  {
        super();
    }

    public Kategorie(String name)  {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Kategorie))  
           throw new ClassCastException();

        Kategorie k = (Kategorie)o;
        String name = k.getName();
        return this.getName().compareTo(name);

    }
}

In a map I saved keys and values of this type "Kategorie".

mapKategorieDEundEN.put(new Kategorie(strName_de), new Kategorie(strName_en));

Later in the code, I want to check if there is a key with a specific string.

if (mapKategorieDEundEN.containsKey(searchString))  {

...doesn't work, because the key is not a string but a "Kategorie", that's clear.

Then I tried something like this:

if (mapKategorieDEundEN.containsKey(new Kategorie(searchString)))  {

...doesn't work too. I assume that it doesn't find anything because the object is not the "original" object but a new one.

In this case, can I use containsKey at all or do I have to use a loop over the HashMap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌︼了一个春 2024-11-21 04:01:29

您的类应该覆盖 等于hashCode,之后就可以工作了。

HashMap/Hashtable 通过使用键的 hashCode 将项目放入“桶”中,因此与另一个对象表示相同值的新对象(并且应被视为同一对象)必须返回相同的 hashCode。所有返回相同 hashCode 的键都将被视为候选键,并且将对它们调用 equals。如果 equals 返回 true,则视为匹配。

You class should override equals and hashCode, it will work after that.

The HashMap/Hashtable puts the items in "buckets" by using the hashCode of the key, so a new object that represents the same value as another object, and which should be considered as the same object must return the same hashCode. All keys that return the same hashCode will then be considered as candidates, and equals will be invoked on them. It's considered a match if equals returns true.

浮云落日 2024-11-21 04:01:29

HashMap 使用hashCode()equals()。你必须实施它们。如果你不知道怎么做。检查你的IDE(eclipse)通常可以为你生成它们。

HashMap uses hashCode() and equals(). You have to implement them. If you don't know how. Check your IDE (eclipse) usually can generate them for you.

晒暮凉 2024-11-21 04:01:29

如果要使用 compareTo 方法访问对象,则不应使用基于 hashCode/equals 的 Map,而应使用 SortedMap,例如 TreeMap(或 ConcurrentSkipListMap)。

这还有一个额外的好处,它可以实现基于范围的查询(例如“给我所有大于这个类别的类别”),但对于简单的 get 来说速度有点慢(O(log n) 而不是 O(1)) 访问与基于散列的访问相比(具有良好的散列码,而不是恒定的散列码)。

对于通用类,定义 hashCode/equals 和 CompareTo 是明智的,然后类的用户可以决定使用哪种类型的映射。 (如果有不同的方法对对象进行排序,最好提供不同的 Comparator 对象。)

顺便说一句,您不应该实现 Comparable,而应该实现 Comparable。那么你的 compareTo 方法将如下所示:

public int compareTo(Kategorie k) {
    String name = k.getName();
    return this.getName().compareTo(name);
}

If you want to access your objects using your compareTo method, you should not use a hashCode/equals based Map, but a SortedMap, like TreeMap (or ConcurrentSkipListMap).

This has the added benefit that it enables range-based queries (e.g. "give me all categories larger than this one"), but is a bit slower (O(log n) instead of O(1)) for simple get accesses compared to hash-based access (with a good hash code, not a constant one).

For a general use class, defining both hashCode/equals and compareTo would be sensible, then the user of the class can decide which type of map to use. (If there are different ways to sort your objects, better provide different Comparator objects.)

As a side remark, you should not implement Comparable, but Comparable<Kategorie>. Then your compareTo method would look like this:

public int compareTo(Kategorie k) {
    String name = k.getName();
    return this.getName().compareTo(name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文