通用深度比较等于 [英] Generic deep compare equals
本文介绍了通用深度比较等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public boolean equals(Ie< T> that){
T This = this.value;
T That = that.value;
boolean x = That.compareTo(This)== 0;
return x;
}
我所做的是:
public boolean equals(Object obj){
if(obj == null)
return false;
String tmp = obj.toString();
if(this.value instanceof Integer)
return equalsInt(new Ie< Integer>(Integer.parseInt(tmp)));
//等
返回false;
}
public boolean equalsInt(Ie< Integer> ie){
Integer This = this.value.intValue();
整数那= ie.value;
boolean x = That.compareTo(This)== 0;
return x;
}
这是一个明显的蛮力解决方案。是否可以在没有 obj.toString(); +新元素的情况下完成?
编辑:
Class Ie被定义为:
public static class Ie< T extends Number&可比< ;? super T>>
扩展Number implements Comparable< Ie< T>> {
类与方法相比相当简单:
public int compareTo(Ie< T> that){
T This = this.value;
T That = that.value;
return(This.compareTo(That)< 0?-1 :( this
.compareTo(That)> 0?1:0));
}
我正在使用Collection addall方法中的类,其中contains调用equals。
Erikson解决方案运行良好:
@Override
public boolean equals(Object that){
if(that == null)
return false;
if(this == that)
return true;
if(that instanceof Ie<>){
Object ThisValue = this.value;
Object ThatValue =((Ie<>)that .value;
return ThisValue.equals(ThatValue);
}
return false;
}
解决方案
public boolean equals(Object that)
{
if(this == that)
return true;
if(that instanceof ie){
Object ThisValue = this.value;
Object ThatValue =((Ie<>)that .value;
return ThisValue.equals(ThatValue);
} else
返回false;
}
Basically i don't want to compare elements, but elements that thy contain. It would be cool if following would work, but it does not.
public boolean equals(Ie<T> that) {
T This = this.value;
T That = that.value;
boolean x = That.compareTo(This) == 0;
return x;
}
What i have done is :
public boolean equals(Object obj) {
if (obj == null)
return false;
String tmp = obj.toString();
if (this.value instanceof Integer)
return equalsInt(new Ie<Integer>(Integer.parseInt(tmp)));
// etc.
return false;
}
public boolean equalsInt(Ie<Integer> ie) {
Integer This = this.value.intValue();
Integer That = ie.value;
boolean x = That.compareTo(This) == 0;
return x;
}
This is a obvious brute-force solution. Can it be done without obj.toString(); + new element?
Edit:
Class Ie is defined as:
public static class Ie<T extends Number & Comparable<? super T>>
extends Number implements Comparable<Ie<T>> {
Class has rather simple compare to method:
public int compareTo(Ie<T> that) {
T This = this.value;
T That = that.value;
return (This.compareTo(That) < 0 ? -1 : (This
.compareTo(That) > 0 ? 1 : 0));
}
I am using the class in Collection addall method, where contains is calling equals.
Erikson solution worked fine:
@Override
public boolean equals(Object that) {
if (that == null)
return false;
if (this == that)
return true;
if (that instanceof Ie<?>) {
Object ThisValue = this.value;
Object ThatValue = ((Ie<?>) that).value;
return ThisValue.equals(ThatValue);
}
return false;
}
解决方案
@Override
public boolean equals(Object that)
{
if (this == that)
return true;
if (that instanceof Ie) {
Object ThisValue = this.value;
Object ThatValue = ((Ie<?>) that).value;
return ThisValue.equals(ThatValue);
} else
return false;
}
这篇关于通用深度比较等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文