java - 使用包含空字段的复合键读取 Multimap

标签 java collections guava multimap

我有一个Multimap来存储一些带有复合键(数据类型、数据ID)的数据。 这是我的代码:

public class Data {

    private final String type;

    private final Integer id;

    private final String information;

    public Data(String type, Integer id, String information) {
        this.type = type;
        this.id = id;
        this.information = information;
    }

    @Override
    public String toString() {
        return "Type: " + type + ", ID: " + id + ", Info: " + information;      
    }

}

public class Test {

    private static Multimap<Key, Data> data = HashMultimap.create();

    static class Key {

        private final String type;

        private final Integer id;

        public Key(String type, Integer id) {
            this.type = type;
            this.id = id;
        }

        @Override
        public int hashCode() {
            //System.out.println("in hashcode");
            return Objects.hashCode(type);
        }

        @Override
        public boolean equals(Object obj) {
            //System.out.println("in equals");
            if (obj == null) {
                return false;
            }

            if (!(obj instanceof Key)) {
                return false;
            }

            Key other = (Key) obj;
            if (other.type.equals(type)) {
                if (other.id == null) {  // Return true for null ID Data
                    return true;
                } else if (other.id.equals(id)) {
                    return true;
                }
            }

            return false;
        }

    }

    private static void addData(String type, Integer id, String information) {
        data.put(new Key(type, id), new Data(type, id, information));
    }

    private static void printData(String type, Integer id) {
        System.out.println("\n===============");
        System.out.println("Filtered Data for: Type => " + type + ", ID => " + id);
        for (Data d : data.get(new Key(type, id))) {
            System.out.println(d);
        }
    }

    public static void main(String[] args) {
        addData("confidential", 11, "Some Confidential Data!");
        addData("private", 21, "Some Private Data!");
        addData("government", 13, "Some Govt. Data!");

        addData("public", null, "Some public data!");
        addData("public", 26, "Another public data!");
        addData("public", 4, "More public information!");

        addData("unspecified", null, "Lorem ipsum dolor sit amet!");
        addData("unspecified", 15, "consectetur adipiscing elit.");
        addData("unspecified", 25, "Integer sed velit vel.");
        addData("unspecified", null, "In vitae velit consequat");       

        printData("unspecified", 25);
        printData("public", 26);
    }

}

现在,我想按 Data::typeData::id 过滤此数据,包括 Data::type 的数据匹配,但 Data::idnull

预期输出:

===============
Filtered Data for: Type => unspecified, ID => 25
Type: unspecified, ID: null, Info: In vitae velit consequat
Type: unspecified, ID: null, Info: Lorem ipsum dolor sit amet!
Type: unspecified, ID: 25, Info: Integer sed velit vel.

===============
Filtered Data for: Type => public, ID => 26
Type: public, ID: null, Info: Some public data!
Type: public, ID: 26, Info: Another public data!

实际输出

===============
Filtered Data for: Type => unspecified, ID => 25
Type: unspecified, ID: null, Info: In vitae velit consequat
Type: unspecified, ID: 15, Info: consectetur adipiscing elit.
Type: unspecified, ID: null, Info: Lorem ipsum dolor sit amet!
Type: unspecified, ID: 25, Info: Integer sed velit vel.

===============
Filtered Data for: Type => public, ID => 26
Type: public, ID: 4, Info: More public information!
Type: public, ID: null, Info: Some public data!
Type: public, ID: 26, Info: Another public data!

为此,我在 Key 类中实现了 hashCode()equals() 方法并返回 true 当其他数据对象 idnull 时。

<小时/>

问题:

  1. 我使用的方法是否正确,或者可以使用简单的Map来实现?

  2. 为什么当我调用 Multimap::get() 方法时,hashCodeequals 方法仅被调用一次? (根据我的说法, map 中的每个键都应该与指定的键匹配!)

  3. 如果这是正确的方法,如何实现 equals 方法以获得预期结果?

最佳答案

为了使 HashMap 正常工作,equals 实现必须满足以下约束:如果 a.equals(b)b.equals(c) then a.equals(c) 必须为真。如果你打破了这个限制,那么 map 的行为就会变得不可预测。这种性质称为传递性。请参阅 equals 实现的完整约束列表 here .

要执行您想要的操作,您必须在 printData 方法中执行两次查找,一次使用 new Key(type, id) 进行查找,另一次使用 new Key(类型,空)。巧妙的 equals 实现没有捷径。

要回答这个问题,为什么当您调用 get() 时,您会看到 hashCodeequals 仅被调用一次?这是 equals 约束的另一个结果。根据您的 equals 实现, map 已确定 Key(x, 15)Key(x, null) 相同。这是在插入数据时完成的。因此,在查找时,它只需与其中一个进行比较,然后假设另一个也匹配。

关于java - 使用包含空字段的复合键读取 Multimap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28853517/

相关文章:

scala - 从 Java 属性获取 Scala 映射

java - Guava 缓存作为 Spring ap 中基于时间的清理存储

java - 更改图像样式会导致触发 LoadEvent 吗?

java - Swing:一列的多个 CellEditor

java - 我可以在 Java 中一次遍历两个数组吗?

Java - 生成特定数字的随机范围而不重复这些数字 - 如何?

c# - 实现依赖对象子属性的自定义集合的正确方法?

java - 是否有用于 Java 的事件驱动的 JSON REST 客户端 API?

Java、谷歌集合库; AbstractIterator 的问题?

java - 知道可达性的缓存