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

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

问题描述

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

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) 当输入为空时我应该返回什么?任何非空字符串在字典上应该比空值大还是小?

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?

这里我可能没有表达清楚,但在我正在执行的程序中,可能为空的字符串都是字段或类,不应为空.换句话说,comparedTo() 使用的对象不能为空,只能是它们的私有字段.所以在这种情况下,我相信如果我正确实现 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)code>,即抛出 NullPointerException),但与如何处理 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), i.e. throw a NullPointerException), 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).

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

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