1

私はHazelast Mapを使用しており、カスタムクラスのオブジェクトであるキーに対してオブジェクトを保存しようとしていますHMapKeyHMapKeyこれがクラスのスニペットです。

public class HMapKey implements Serializable{
    private String keyCode;
    private long time;

    public HMapKey(String keyCode, long time) {
      this.keyCode = keyCode;
      this.time = time;
    }
    public String getKeyCode() {
        return keyCode;
    }
    public void setKeyCode(String keyCode) {
        this.keyCode = keyCode;
    }
    public long getTime() {
        return time;
    }
    public void setTime(long time) {
        this.time = time;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((keyCode == null) ? 0 : keyCode.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        HMapKey other = (HMapKey) obj;
        if (keyCode == null) {
            if (other.keyCode != null)
                return false;
        } else if (!keyCode.equals(other.keyCode))
            return false;
        return true;
    }
}

上記のコードでわかるように、keyCode2 つのオブジェクトを比較するために equals() メソッドでのみ使用しています。timeしたがって、変数にどんな値があっても構いません。しかし、このオブジェクトを Hazelcast の Map のキーとして使用して取得しようとすると、Hazelcast はキー クラスのすべての変数をチェックしているとkeyCode思いtimeますNULL。Hazelcast にすべての変数チェックを行わず、既存のequals()メソッドのみを使用するように指示する構成はありますか?

マップからデータを取得しようとしているコードは次のとおりです

HazelcastInstance instance = Hazelcast.newHazelcastInstance();
private static ConcurrentMap<HMapKey, String>   testMap = instance.getMap("TEST_MAP");
testMap.put(new HMapKey("code1",123), "This is Code 1");
System.out.println(testMap.get(new HMapKey("code1",0)));

つまり、挿入中にキーオブジェクトを作成しましnew HMapKey("code1",123)たが、取得中に新しいオブジェクトを作成しnew HMapKey("code1",0)ていて、null 値として返されています。一方、試してみるとうまくいきnew HMapKey("code1",123)ます。

4

3 に答える 3

2

まず、「回収してみる」というと、何をしようとしているのかわかりません。

get メソッドのようにキーを使用すると、すべて正常に動作します。

@Test
public void test() {
    HazelcastInstance hz = createHazelcastInstance();

    IMap<HMapKey, Integer> map = hz.getMap("aaa");
    HMapKey key = new HMapKey();
    key.keyCode = "code1";
    key.time = 123;

    HMapKey key2 = new HMapKey();
    key2.keyCode = "code2";
    key2.time = 246;

    map.put(key, 1);
    map.put(key2, 2);

    int value = map.get(key);
    assertEquals(value, 1);
}

キー値全体に基づいて値を取得する場合は、HMapKey を実装する必要がありますComparable

次に、次のようにクエリできます。

map.values(Predicates.equal("__key", yourHKMapKeyInstance));

于 2016-12-20T09:18:16.147 に答える
1

Hazelcast は、キー/値を逆シリアル化しequalsたり、メソッドを実行したりしませんがhashCode、シリアル化されたオブジェクトを対応するバイトストリームで比較します。1 つ以上のプロパティを検索している場合は、Tom https://stackoverflow.com/a/41238649/917336からの回答を参照してください。

于 2016-12-20T10:41:56.587 に答える
1

timevariableを として宣言することで、それを実現できますtransient。ただし、衝突につながり、ランダムな結果になることに注意してください。プロパティが異なっていても、プット操作は以前の値を上書きします。

HazelcastInstance instance = Hazelcast.newHazelcastInstance();
IMap<HMapKey, String> testMap = instance.getMap("TEST_MAP");

testMap.put(new HMapKey("code1",123), "This is Code 1");

System.out.println("HMapKey with time=0: " + testMap.get(new HMapKey("code1",0)));
System.out.println("HMapKey with time=123: " + testMap.get(new HMapKey("code1",123)));


testMap.put(new HMapKey("code1",456), "This is Code 2");
System.out.println("HMapKey with time=123: " + testMap.get(new HMapKey("code1",123)));

出力は次のようになります。

HMapKey with time=0: This is Code 1
HMapKey with time=123: This is Code 1
HMapKey with time=123: This is Code 2
于 2016-12-20T11:26:54.190 に答える