当参数字符串为null时,int compareTo()应该返回什么? [英] What should int compareTo() return when the parameter string is null?

查看:257
本文介绍了当参数字符串为null时,int compareTo()应该返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说当输入参数为null时,compareTo()应抛出NullPointerException。但是,我正在实现一个需要将字段与String类型进行比较的类。这些字段不必是强制性的。我想知道在这种情况下,

It is said that when input parameter is null, compareTo() should throw a NullPointerException. However, I am implementing a class which needs to compare fields with the type of String. These fields need not to be mandatory. I wonder in this case,

1)当输入为空时我应该返回什么?是否任何非空字符串按字典顺序大于或小于null?

1) What should I return when the input is null? Should any not-null strings lexicographically bigger or smaller than null?

2)如果考虑到这一点不好的做法,是否有任何支持性论点?我应该强制用户使用空字符串吗?如果使用空字符串,那不会混淆字段不适用的情况和字段为空的情况吗?如果必须抛出异常,那么除了在手册中警告用户之外,还有什么可以/我应该做什么?

2) If this is considered bad practice, is there any supporting arguments? Should I force the user to use empty strings instead? If using empty string, won't that confuse the case in which the field is not applicable and the case in which the field is empty? And if exception must be thrown, then except from warning the user in the manual, what else could/shall I do?

编辑:我可能不会在这里表达清楚,但在我正在实现的程序中,可以为null的字符串是所有字段或类,它们不应为null。换句话说,comparisonTo()使用的对象不能为null,只有它们的私有字段可以。所以在这种情况下,我相信如果我正确地实现了compareTo(),它就不会违反传递要求,因为具有空字段的类总是被认为是相同的。我是对的还是我在解释这个错误?

I might not express myself clearly here, but in the program I am implementing, the strings that could be null are all fields or a class, which should not be null. In other words, the objects comparedTo() uses could not be null, just their private fields could be. So in this case, I believe if I implement compareTo() properly, it would not violate the transitive requirement since classes with null fields would be considered the same always. Am I right or am I interpreting this wrong?

谢谢大家的答案!

推荐答案

是的,对于实例字段允许 null 没有问题 - 只需确保定义其排序顺序。最自然的是将它放在所有真正的琴弦之前或之后,但你可以在这里做任何事情,只要一贯地做。 (例如,您可以将 null 排序为null。)

Yes, there is no problem allowing null for instance fields - just make sure its sorting order is defined. Most natural would be putting it either before or after all real strings, but you could do anything here, just do it consistently. (For example, you could sort null like "null".)

以下是单个成员的示例实现:

Here is an example implementation for a single member:

class Example implements Comparable<Example> {

   @Nullable
   private String member;

   // TODO: getter, setter, constructor, ...

   public int compareTo(Example that) {
      if(this.member == null)
         if(that.member == null)
            return 0; //equal
         else
            return -1; // null is before other strings
       else // this.member != null
         if(that.member == null)
            return 1;  // all other strings are after null
         else
            return this.member.compareTo(that.member);
   }
}

请注意Comparable.compareTo()的规格只有 o.compareTo(null)的约束(它的行为应该像 - null.compareTo(o) ),但不是关于如何处理 null 字段(它根本没有提到字段,所以只要反对称性,反身性和反对性,类就可以返回它想要的任何东西。确保传递性。)

Please note that the specification of Comparable.compareTo() only has a constraint for o.compareTo(null) (which should behave just like - null.compareTo(o)), but not about how null fields are handled (it doesn't mention fields at all, so a class could return whatever it wants, as long as the antisymmetry, reflexivity and transitivity is ensured).

这篇关于当参数字符串为null时,int compareTo()应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆