为什么Map的一般契约在比较对象时要求使用equals方法,这在hazelcast Map中是违反的

时间:2015-10-07 13:37:10

标签: java hazelcast distributed-caching distributed-cache hazelcast-imap

这更像是一个设计问题。我想知道为什么hazelcast使用对象的二进制(序列化)形式的 import java.io.*; import java.util.*; public class LineCalculator { public static void main(String[] arr){ System.out.println("Enter the name of the input file"); Scanner scanner = new Scanner(System.in); String file = scanner.nextLine(); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; int sum = 0; double average = 0; int totallength = 0; String shortestline = new String(); String longestline = new String(); while ((line = br.readLine()) != null) { sum++; if (shortestline.length() > line.length()||line!=null){ shortestline = line; } if(longestline.length() < line.length()){ longestline = line; } int temp = line.length(); totallength = totallength + temp; average = (double) Math.round(100*totallength/(double)sum)/100; } System.out.println("Shortest line: "+shortestline); System.out.println("Longest line: "+longestline); System.out.println("Average lenght of the lines: "+ average); } catch (FileNotFoundException e) { System.out.println("File not found, panic!"); } catch (IOException e) { System.out.println("The input of the file is not compatible"); e.printStackTrace(); } } } hashCode(),而不是复合键对象提供的形式。

我遇到了一个问题,我必须将一些元数据(equals()lastModifiedTimeStamp等)与密钥对象相关联,这是因为此违规而无法实现的。

虽然我确实知道还有其他方法可以解决我的问题,但是将这些属性作为密钥的一部分(然后覆盖lastModifiedNode / hashCode()方法以排除元数据)将会是一种更清洁的方法。

1 个答案:

答案 0 :(得分:1)

这是一个非常好的问题。这有多种原因,但其中一个原因可能是:

我们将密钥保存为序列化(=二进制)形式。在其他使用来自域对象的常规hashCode()和equals()时,我们需要以反序列化(对象)格式使用它们。

仅这一事实就意味着: - 您将始终需要所有集群成员上的域对象的.class文件。当成员只处理序列化blob时,你就不需要它了。

  • 您可以将密钥保留为反序列化格式,但是您必须为每个远程请求序列化它们 - &gt;性能惩罚。 Hazelcast建立在假设大多数操作都是远程的基础上。

  • 或者您可以在序列化和&amp;反序列化形式 - &gt;太空惩罚。

我确定还有其他原因;这些只是我能想到的一些问题。

致谢 - Jaromir Hamala - Hazelcast邮件列表。