HashMultimap Guava Java,从关键值获取集合的问题

时间:2022-02-18 16:51:27

Hopefully I understand the purpose for the HashMultimap in Guava, because if I don't, I'll just downvote myself.

希望我理解Guava中HashMultimap的目的,因为如果我不这样做,我只会自己动手。

I'm trying to access the Collection from a specific key where the key is a class like so...

我正试图从一个特定的键访问Collection,其中键是一个类似的类......

public class Coords {

private int[] coords;

public Coords() {
    coords = new int[2];
}

public Coords(int x, int y) {
    coords = new int[] {x, y};
}

public void set(int x, int y) {
    coords = new int[] {x, y};
}

public int x() {
    return coords[0];
}

public int y() {
    return coords[1];
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null || getClass() != obj.getClass())
        return false;

    Coords o = (Coords) obj;

    return Integer.compare(x(), o.x()) == 0 && 
           Integer.compare(y(), o.y()) == 0;
}

When I compare two Coords objects with the same integer values in it's array, I get true.

当我在它的数组中比较两个Coords对象具有相同的整数值时,我得到了真的。

When I popular the HashMultimap with key/value pairs, I do indeed get a unique set of keys, but I do not get a collection of more than one Item. I get multiple keys that seem to be identical, even though I have overridden the equals() method in Object. When I popular the map...

当我使用键/值对来使用HashMultimap时,我确实得到了一组唯一的键,但是我没有得到多个Item的集合。我得到了多个看似相同的键,即使我已经覆盖了Object中的equals()方法。当我流行地图时......

HashMultimap<Coords, Item> items = HashMultimap.create();

        Item s = new Item();
        s.coords.set(0, 0);
        Item w = new Item();
        w.coords.set(0, 0);

        Item p = new Item();
        p.coords.set(1, 1);

        items.put(s.coords, s);
        items.put(w.coords, w);
        items.put(p.coords, p);

        Collection<Item> bucket = items.get(s.coords);
        bucket.add(s);
        items.putAll(s.coords, bucket);

        bucket = items.get(w.coords);
        bucket.add(w);
        items.putAll(w.coords, bucket);

        bucket = items.get(p.coords);
        bucket.add(p);
        items.putAll(p.coords, bucket);

        for(Coords key : items.keySet()) {
            System.out.println(key.x() + " " + key.y());
        }

I get the output...

我得到了输出......

0 0
1 1
0 0

What am I missing? Did I implement something incorrectly?

我错过了什么?我做错了什么吗?

1 个解决方案

#1


0  

Thanks for all the comments. I actually realized I need to overwrite the hashCode() method shortly after I posted this question. Of course...

感谢所有的评论。我实际上意识到我需要在发布此问题后立即覆盖hashCode()方法。当然...

But I will explain, for those of you who are stuck on this problem, and don't understand how to implement hashCode() successfully. This question has answers very similar to mine...

但是我会解释,对于那些坚持这个问题的人,并且不理解如何成功实现hashCode()。这个问题的答案与我的非常相似......

Does hashcode implementation of Java Arrays.hashcode() uniformly distribute

Java Arrays.hashcode()的hashcode实现是否均匀分布

But, I ended up implementing hashCode() like this...

但是,我最终实现了像这样的hashCode()......

@Override
public int hashCode() {
    int hash = 1;
    for (int e : coords)
        hash = 31 * hash + e;

    return hash;
}

Thanks to cpp beginner, I could have used Arrays.hashCode(), but just thought I'd write out something so that I could see it working. So I could better understand the method and what it does.

感谢cpp初学者,我本来可以使用Arrays.hashCode(),但只是想我会写出一些东西,以便我能看到它正常工作。所以我可以更好地理解方法及其作用。

#1


0  

Thanks for all the comments. I actually realized I need to overwrite the hashCode() method shortly after I posted this question. Of course...

感谢所有的评论。我实际上意识到我需要在发布此问题后立即覆盖hashCode()方法。当然...

But I will explain, for those of you who are stuck on this problem, and don't understand how to implement hashCode() successfully. This question has answers very similar to mine...

但是我会解释,对于那些坚持这个问题的人,并且不理解如何成功实现hashCode()。这个问题的答案与我的非常相似......

Does hashcode implementation of Java Arrays.hashcode() uniformly distribute

Java Arrays.hashcode()的hashcode实现是否均匀分布

But, I ended up implementing hashCode() like this...

但是,我最终实现了像这样的hashCode()......

@Override
public int hashCode() {
    int hash = 1;
    for (int e : coords)
        hash = 31 * hash + e;

    return hash;
}

Thanks to cpp beginner, I could have used Arrays.hashCode(), but just thought I'd write out something so that I could see it working. So I could better understand the method and what it does.

感谢cpp初学者,我本来可以使用Arrays.hashCode(),但只是想我会写出一些东西,以便我能看到它正常工作。所以我可以更好地理解方法及其作用。