Java:实现可比较但条件 if 太多。我怎样才能避免它们?

发布于 2024-12-28 09:50:31 字数 865 浏览 0 评论 0原文

我有一个实现Comparable的对象列表

我想对此列表进行排序,这就是我使用 Comparable 的原因。

每个对象都有一个字段 weight,它由另外 3 个成员 int 变量组成。

对于具有最大权重的对象,compareTo 返回1
最重要的不仅是 if ,

weightObj1.member1 > weightObj2.member1  
weightObj1.member2 > weightObj2.member2  
weightObj1.member3 > weightObj2.member3

而且实际上有点复杂,我最终得到的代码有太多条件 if 。

如果weightObj1.member1> WeightObj2.member1 成立,那么我关心是否 weightObj1.member2 > WeightObj2.member2。

反之亦然。

否则如果 weightObj1.member2 > WeightObj2.member2 成立,那么我关心是否 weightObj1.member3 > WeightObj2.member3 反之亦然。

最后如果weightObj1.member3> WeightObj2.member3 保持 AND 如果满足特定条件,则此 weightObj1 获胜,反之亦然

我想知道是否有类似这样的设计方法?

I have a list of objects which implement Comparable.

I want to sort this list and that is why I used the Comparable.

Each object has a field, weight that is composed of 3 other member int variables.

The compareTo returns 1 for the object with the most weight.
The most weight is not only if the

weightObj1.member1 > weightObj2.member1  
weightObj1.member2 > weightObj2.member2  
weightObj1.member3 > weightObj2.member3

but actually is a little more complicated and I end up with code with too many conditional ifs.

If the weightObj1.member1 > weightObj2.member1 holds then I care if weightObj1.member2 > weightObj2.member2.

and vice versa.

else if weightObj1.member2 > weightObj2.member2 holds then I care if weightObj1.member3 > weightObj2.member3 and vice versa.

Finally if weightObj1.member3 > weightObj2.member3 holds AND if a specific condition is met then this weightObj1 wins and vice versa

I was wondering is there a design approach for something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水水月牙 2025-01-04 09:50:31

您可以尝试使用 Apache commons-lang< 中的 CompareToBuilder /a>:

public int compareTo(Object o) {
   MyClass myClass = (MyClass) o;
   return new CompareToBuilder()
     .appendSuper(super.compareTo(o)
     .append(this.field1, myClass.field1)
     .append(this.field2, myClass.field2)
     .append(this.field3, myClass.field3)
     .toComparison();
}

另请参阅

You can try with CompareToBuilder from Apache commons-lang:

public int compareTo(Object o) {
   MyClass myClass = (MyClass) o;
   return new CompareToBuilder()
     .appendSuper(super.compareTo(o)
     .append(this.field1, myClass.field1)
     .append(this.field2, myClass.field2)
     .append(this.field3, myClass.field3)
     .toComparison();
}

See also

神仙妹妹 2025-01-04 09:50:31

与上述 Apache CompareToBuilder 类似,但包括泛型支持,Guava 提供 ComparisonChain:

public int compareTo(Foo that) {
  return ComparisonChain.start()
     .compare(this.aString, that.aString)
     .compare(this.anInt, that.anInt)
     .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
          // you can specify comparators
     .result();
}

Similar to the above-mentioned Apache CompareToBuilder, but including generics support, Guava provides ComparisonChain:

public int compareTo(Foo that) {
  return ComparisonChain.start()
     .compare(this.aString, that.aString)
     .compare(this.anInt, that.anInt)
     .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
          // you can specify comparators
     .result();
}
不顾 2025-01-04 09:50:31

用于Comparable的 API 指出:

强烈建议(尽管不是必需)自然的
顺序与 equals 一致。

由于感兴趣的值是 int 值,因此您应该能够得出一个值来捕获比较两个对象所需的所有比较和其他转换。当任何成员值发生更改时,只需更新单个值即可。

The API for Comparable states:

It is strongly recommended (though not required) that natural
orderings be consistent with equals.

Since the values of interest are int values you should be able to come up with a single value that captures all comparisons and other transformations you need to compare two of your objects. Just update the single value when any of the member values change.

儭儭莪哋寶赑 2025-01-04 09:50:31

您可以尝试使用反射,迭代属性并比较它们。

You can try using reflection, iterate over properties and compare them.

美人如玉 2025-01-04 09:50:31

你可以尝试这样的事情:

int c1 = o1.m1 - o2.m1;
if (c1 != 0) {
    return c1;
}

int c2 = o1.m2 - o2.m2;
if (c2 != 0) {
    return c2;
}

return o1.m3 - o2.m3;

因为可比较不仅仅返回-1、0或1。它可以返回任何整数值,并且只考虑符号。

You can try something like this:

int c1 = o1.m1 - o2.m1;
if (c1 != 0) {
    return c1;
}

int c2 = o1.m2 - o2.m2;
if (c2 != 0) {
    return c2;
}

return o1.m3 - o2.m3;

because comparable shall not just return -1, 0 or 1. It can return any integer value and only the sign is considered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文