Osc*_*Ryz 11 java eclipse hash code-generation intellij-idea
我记得eclipse和idea有这个模板根据其属性自动创建一个对象的hashCode.
如果使用数字和字符串,其中一种策略是这样的.
return stringValue.hashCode() + intValue * 32;
Run Code Online (Sandbox Code Playgroud)
这样的东西.
我手头没有也没有eclipse或想法,我想创建这样的功能.
编辑
根据答案,我创建了这个迷你课程
class StringInt {
private final String s;
private final int i;
static StringInt valueOf( String string , int value ) {
return new StringInt( string, value );
}
private StringInt( String string, int value ) {
this.s = string;
this.i = value;
}
public boolean equals( Object o ) {
if( o != null && o instanceof StringInt ){
StringInt other = ( StringInt ) o;
return this.s == other.s && this.i == other.i;
}
return false;
}
public int hashCode() {
return s != null ? s.hashCode() * 37 + i : i;
}
}
Run Code Online (Sandbox Code Playgroud)
这个类将用作大内存映射的键(> 10k元素)我不想每次迭代它们以查找String和int是否相同.
谢谢.
ps .. mmh可能它应该是StringIntKey的名字.
使用Apache Commons HashcodeBuilder:
public int hashCode() {
new HashCodeBuilder(17, 37).
append(myString).
append(myInt);
}
Run Code Online (Sandbox Code Playgroud)
链接到这里:http: //commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/HashCodeBuilder.html
和这里:
http://www.koders.com/java/fidCE4E86F23847AE93909CE105394B668DDB0F491A.aspx
归档时间: |
|
查看次数: |
20712 次 |
最近记录: |