问题描述
由于我的数学技能不高,我问你是否存在我将来可能会在课堂上使用的算法?
Since I don't have any great skills in math, I ask you if there exists any algorithm that I should use for a class which probably will change in the future.
请考虑以下情形:
角色"类具有以下字段:
Class "Roles" has following fields:
private boolean admin;
private boolean printer;
几周后,我决定添加一个来宾"角色:
After some weeks I decide to add a role "guest":
private boolean admin;
private boolean printer;
private boolean guest;
几周后,我决定删除打印机"角色;
After some weeks I decide to remove the role "printer";
private boolean admin;
private boolean guest;
由于我将哈希码保留在数据库中,因此我必须100%确保此类的所有版本都生成唯一的哈希码.
Since I will persist the hashcode in a database, I must be 100% sure that all versions of this class generates unique hashcodes.
也许这不是问题,我一直使用Eclispe IDE源代码生成器中提供的代码.
Maybe this is not a problem, I have always used the one provided in the Eclispe IDE source generator.
能否请您告诉我使用Eclipse IDE(Indigo)Java版本> = 6的方法是否安全,或者给我有关此主题的其他建议.我相信这是很普遍的事情.
Can you please tell me if I am safe with the Eclipse IDE (Indigo) Java version >= 6 method or give me some other advices regarding this topic. I am sure this is a very common thing.
预先感谢
推荐答案
不要那样做. hashCode
的结果不是 meant 要保留的.特别是从文档中:
Don't do that. The result of hashCode
isn't meant to be persisted. In particular, from the docs:
下一步:
哈希码也不是唯一的……它们经常不会.好的,您的案例中只有5位数据,但通常情况并非如此...
Hash codes aren't meant to be unique, either... they very often won't be. Okay, you've only got 5 bits of data in your case, but in general that's not the case...
听起来您对Object.hashCode()
的要求与正常要求不同-因此,您不应期望任何自动生成的实现都知道您的特殊要求.我建议您准确地说明您的要求,然后我们可以确定该怎么做...
It sounds like you have different requirements from the normal ones for Object.hashCode()
- so you shouldn't expect any autogenerated implementation to know about your special requirements. I suggest you state exactly what your requirements are, and we can work out what to do...
这篇关于可扩展类的哈希码(面向未来)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!