CompareTo() 与 equals()

发布于 2024-08-07 04:18:09 字数 470 浏览 3 评论 0原文

当在 Java 中测试 String 的相等性时,我总是使用 equals() 因为对我来说这似乎是最自然的方法。毕竟,它的名字已经说明了它的用途。然而,我的一位同事最近告诉我,有人教我使用 compareTo() == 0 而不是 equals()。这感觉不自然(因为 compareTo() 旨在提供排序而不是比较相等性),甚至有些危险(因为 compareTo() == 0 并不一定意味着在所有情况下都是平等的,尽管我知道它对我来说对 String 来说是平等的。

他不知道为什么他被教导使用 compareTo() 而不是 equals() 作为 String 的,我也找不到任何原因。这真的是个人品味的问题,还是这两种方法都有真正的理由?

When testing for equality of String's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0 instead of equals(). This feels unnatural (as compareTo() is meant to provide an ordering and not compare for equality) and even somewhat dangerous (because compareTo() == 0 does not necessarily imply equality in all cases, even though I know it does for String's) to me.

He did not know why he was taught to use compareTo() instead of equals() for String's, and I could also not find any reason why. Is this really a matter of personal taste, or is there any real reason for either method?

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

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

发布评论

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

评论(21

毁我热情 2024-08-14 04:18:09

不同之处在于 "foo".equals((String)null) 返回 false,而 "foo".compareTo((String)null) == 0 抛出 NullPointerException。因此,即使对于字符串,它们也不总是可以互换的。

A difference is that "foo".equals((String)null) returns false while "foo".compareTo((String)null) == 0 throws a NullPointerException. So they are not always interchangeable even for Strings.

白云不回头 2024-08-14 04:18:09

两个主要区别是:

  1. equals 将接受任何对象作为参数,但 compareTo 将仅接受字符串。
  2. equals 仅告诉您它们是否相等,但 compareTo 提供有关字符串如何按字典顺序比较的信息。

我查看了 String 类代码,并且CompareTo 和 equals 中的算法看起来基本相同。我相信他的观点只是一个品味问题,我同意你的观点——如果你需要知道的是字符串的相等性,而不是按字典顺序哪个是第一个,那么我会使用equals

The 2 main differences are that:

  1. equals will take any Object as a parameter, but compareTo will only take Strings.
  2. equals only tells you whether they're equal or not, but compareTo gives information on how the Strings compare lexicographically.

I took a look at the String class code, and the algorithm within compareTo and equals looks basically the same. I believe his opinion was just a matter of taste, and I agree with you -- if all you need to know is the equality of the Strings and not which one comes first lexicographically, then I would use equals.

满身野味 2024-08-14 04:18:09

在比较相等性时,您应该使用equals(),因为它以清晰的方式表达您的意图。

compareTo() 还有一个缺点,即它仅适用于实现 Comparable 接口的对象。

这适用于一般情况,而不仅仅是字符串。

When comparing for equality you should use equals(), because it expresses your intent in a clear way.

compareTo() has the additional drawback that it only works on objects that implement the Comparable interface.

This applies in general, not only for Strings.

素衣风尘叹 2024-08-14 04:18:09

如果字符串具有不同的长度,则 compareTo 会做更多的工作。 equals 只能返回 false,而 compareTo 必须始终检查足够的字符来找到排序顺序。

compareTo has do do more work if the strings have different lengths. equals can just return false, while compareTo must always examine enough characters to find the sorting order.

等风也等你 2024-08-14 04:18:09

在字符串上下文中:
compareTo:按字典顺序比较两个字符串。
equals:将此字符串与指定对象进行比较。

compareTo 按字符(位于相同索引)比较两个字符串,并相应地返回一个整数(正数或负数)。

String s1 = "ab";
String s2 = "ab";
String s3 = "qb";
s1.compareTo(s2); // is 0
s1.compareTo(s3); // is -16
s3.compareTo(s1); // is 16

In String Context:
compareTo: Compares two strings lexicographically.
equals: Compares this string to the specified object.

compareTo compares two strings by their characters (at same index) and returns an integer (positive or negative) accordingly.

String s1 = "ab";
String s2 = "ab";
String s3 = "qb";
s1.compareTo(s2); // is 0
s1.compareTo(s3); // is -16
s3.compareTo(s1); // is 16
虐人心 2024-08-14 04:18:09

compareTo() 不仅适用于字符串,还适用于任何其他对象,因为 compareTo 采用通用参数 T。 String 是通过实现 Comparable 接口来实现 compareTo() 方法的类之一。(compareTo() 是比较接口的方法)。 所以任何类都可以自由地实现 Comparable 接口。

但是 compareTo() 给出了对象的排序,通常用于按升序或降序对对象进行排序,而 equals() 只会谈平等,说平等与否。

compareTo() not only applies to Strings but also any other object because compareTo<T> takes a generic argument T. String is one of the classes that has implemented the compareTo() method by implementing the Comparable interface.(compareTo() is a method fo the comparable Interface). So any class is free to implement the Comparable interface.

But compareTo() gives the ordering of objects, used typically in sorting objects in ascending or descending order while equals() will only talk about the equality and say whether they are equal or not.

噩梦成真你也成魔 2024-08-14 04:18:09

equals()compareTo() 更高效。

CompareTo 和 equals 之间非常重要的区别:

"myString".compareTo(null);  //Throws java.lang.NullPointerException
"myString".equals(null);     //Returns false

equals() 检查两个对象是否相同并返回一个布尔值。

compareTo()(来自 Comparable 接口)返回一个整数。它检查两个对象中的哪一个“小于”、“等于”或“大于”另一个。并非所有对象都可以按逻辑顺序排序,因此compareTo() 方法并不总是有意义。

请注意,equals() 不定义对象之间的顺序,而compareTo() 则定义。

现在我建议您查看这两种方法的源代码,得出结论,equals 比涉及一些数学计算的compareTo 更可取。

equals() can be more efficient then compareTo().

A very important difference between compareTo and equals:

"myString".compareTo(null);  //Throws java.lang.NullPointerException
"myString".equals(null);     //Returns false

equals() checks if two objects are the same or not and returns a boolean.

compareTo() (from interface Comparable) returns an integer. It checks which of the two objects is "less than", "equal to" or "greater than" the other. Not all objects can be logically ordered, so a compareTo() method doesn't always make sense.

Note that equals() doesn't define the ordering between objects, which compareTo() does.

Now I advise you to review the source code of both methods to conclude that equals is preferable over compareTo that involves some Math calculations.

梦冥 2024-08-14 04:18:09

看起来这两种方法几乎做同样的事情,但compareTo()方法接受一个字符串,而不是一个对象,并在正常的equals()方法之上添加了一些额外的功能。如果您关心的只是相等,那么 equals() 方法是最佳选择,因为它对于下一个查看您的代码的程序员来说更有意义。除非您循环遍历大量项目,否则两个不同函数之间的时间差应该不重要。当您需要知道集合中字符串的顺序或者需要知道以相同字符序列开头的字符串之间的长度差异时,compareTo() 非常有用。

来源: http://java.sun.com/javase /6/docs/api/java/lang/String.html

It appears that both methods pretty much do the same thing, but the compareTo() method takes in a String, not an Object, and adds some extra functionality on top of the normal equals() method. If all you care about is equality, then the equals() method is the best choice, simply because it makes more sense to the next programmer that takes a look at your code. The time difference between the two different functions shouldn't matter unless you're looping over some huge amount of items. The compareTo() is really useful when you need to know the order of Strings in a collection or when you need to know the difference in length between strings that start with the same sequence of characters.

source: http://java.sun.com/javase/6/docs/api/java/lang/String.html

高速公鹿 2024-08-14 04:18:09

对于OP来说,equals()应该是选择的方法。

查看 java.lang.String on grepcode,我们可以很容易看出,如果我们只关心两个字符串的相等性,则 equals 更好:

equals():

1012  public boolean equals(Object anObject) {
1013 if (this == anObject) {
1014 return true;
1015 }
1016 if (anObject instanceof String) {
1017 String anotherString = (String)anObject;
1018 int n = count;
1019 if (n == anotherString.count) {
1020 char v1[] = value;
1021 char v2[] = anotherString.value;
1022 int i = offset;
1023 int j = anotherString.offset;
1024 while (n-- != 0) {
1025 if (v1[i++] != v2[j++])
1026 return false;
1027 }
1028 return true;
1029 }
1030 }
1031 return false;
1032 }

compareTo():

1174  public int compareTo(String anotherString) {
1175 int len1 = count;
1176 int len2 = anotherString.count;
1177 int n = Math.min(len1, len2);
1178 char v1[] = value;
1179 char v2[] = anotherString.value;
1180 int i = offset;
1181 int j = anotherString.offset;
1183 if (i == j) {
1184 int k = i;
1185 int lim = n + i;
1186 while (k < lim) {
1187 char c1 = v1[k];
1188 char c2 = v2[k];
1189 if (c1 != c2) {
1190 return c1 - c2;
1191 }
1192 k++;
1193 }
1194 } else {
1195 while (n-- != 0) {
1196 char c1 = v1[i++];
1197 char c2 = v2[j++];
1198 if (c1 != c2) {
1199 return c1 - c2;
1200 }
1201 }
1202 }
1203 return len1 - len2;
1204 }

当其中一个字符串是以下前缀时另外,compareTo() 的性能更差,因为它仍然需要确定字典顺序,而 equals() 不会再担心并立即返回 false。

在我看来,我们应该按预期使用这两个:

  • equals() 来检查相等性,
  • compareTo() 来查找词汇顺序。

equals() should be the method of choice in the case of the OP.

Looking at the implementation of equals() and compareTo() in java.lang.String on grepcode, we can easily see that equals is better if we are just concerned with the equality of two Strings:

equals():

1012  public boolean equals(Object anObject) {
1013 if (this == anObject) {
1014 return true;
1015 }
1016 if (anObject instanceof String) {
1017 String anotherString = (String)anObject;
1018 int n = count;
1019 if (n == anotherString.count) {
1020 char v1[] = value;
1021 char v2[] = anotherString.value;
1022 int i = offset;
1023 int j = anotherString.offset;
1024 while (n-- != 0) {
1025 if (v1[i++] != v2[j++])
1026 return false;
1027 }
1028 return true;
1029 }
1030 }
1031 return false;
1032 }

and compareTo():

1174  public int compareTo(String anotherString) {
1175 int len1 = count;
1176 int len2 = anotherString.count;
1177 int n = Math.min(len1, len2);
1178 char v1[] = value;
1179 char v2[] = anotherString.value;
1180 int i = offset;
1181 int j = anotherString.offset;
1183 if (i == j) {
1184 int k = i;
1185 int lim = n + i;
1186 while (k < lim) {
1187 char c1 = v1[k];
1188 char c2 = v2[k];
1189 if (c1 != c2) {
1190 return c1 - c2;
1191 }
1192 k++;
1193 }
1194 } else {
1195 while (n-- != 0) {
1196 char c1 = v1[i++];
1197 char c2 = v2[j++];
1198 if (c1 != c2) {
1199 return c1 - c2;
1200 }
1201 }
1202 }
1203 return len1 - len2;
1204 }

When one of the strings is a prefix of another, the performance of compareTo() is worse as it still needs to determine the lexicographical ordering while equals() won't worry any more and return false immediately.

In my opinion, we should use these two as they were intended:

  • equals() to check for equality, and
  • compareTo() to find the lexical ordering.
歌枕肩 2024-08-14 04:18:09

equals() 检查两个字符串是否相等。它给出布尔值。
CompareTo() 检查字符串对象是否等于、大于或小于另一个字符串对象。它给出的结果如下:
如果字符串对象更大则为 1
如果两者相等则为 0
-1 如果字符串小于其他字符串

eq:

String a = "Amit";
String b = "Sumit";
String c = new String("Amit");
System.out.println(a.equals(c));//true
System.out.println(a.compareTo(c)); //0
System.out.println(a.compareTo(b)); //1

equals() checks whether two strings are equal or not.It gives boolean value.
compareTo() checks whether string object is equal to,greater or smaller to the other string object.It gives result as :
1 if string object is greater
0 if both are equal
-1 if string is smaller than other string

eq:

String a = "Amit";
String b = "Sumit";
String c = new String("Amit");
System.out.println(a.equals(c));//true
System.out.println(a.compareTo(c)); //0
System.out.println(a.compareTo(b)); //1
抽个烟儿 2024-08-14 04:18:09

在Java中重写compareTo时需要记住一些事情,例如Compareto必须与equals一致,并且减法不应该用于比较整数字段,因为它们可能会溢出。检查 在 Java 中重写 Comparator 时要记住的事情了解详情。

There are certain things which you need to keep in mind while overriding compareTo in Java e.g. Compareto must be consistent with equals and subtraction should not be used for comparing integer fields as they can overflow. check Things to remember while overriding Comparator in Java for details.

岁月染过的梦 2024-08-14 04:18:09
  1. equals 可以接受任何对象作为参数,但 compareTo 只能接受字符串。

  2. 当为 null 时,compareTo 会抛出异常

  3. 当你想知道差异在哪里时发生这种情况时,可以使用compareTo

  1. equals can take any Object as a parameter but compareTo can only take String.

  2. when cometo null,compareTo will throw a exception

  3. when you want to know where the diff happen,you can use compareTo.

做个ˇ局外人 2024-08-14 04:18:09

这是一个死灵术实验 :-)

大多数答案都会比较性能和 API 差异。他们忽略了一个基本点:这两个操作只是具有不同的语义。

你的直觉是正确的。 x.equals(y) 不能与 x.compareTo(y) == 0 互换。
第一个比较身份,而另一个比较“大小”的概念。确实,在许多情况下,尤其是对于原始类型,这两者是共同对齐的。

一般情况是这样的:

如果 x 和 y 相同,则它们共享相同的“大小”:如果 x.equals(y) 为 true => x.compareTo(y) 为 0。

但是,如果 x 和 y 大小相同,并不意味着它们相同。

如果 x.compareTo(y) 为 0 并不一定意味着 x.equals(y) 为 true。

身份与大小不同的一个令人信服的例子是复数。假设通过它们的绝对值进行比较。因此,给定两个复数:Z1 = a1 + b1*i 和 Z2 = a2 + b2*i:

Z1.equals(z2) 当且仅当 a1 = a2 且 b1 = b2 时返回 true。

然而,Z1.compareTo(Z2) 对于无限数量的 (a1,b1) 和 (a2,b2) 对返回 0,只要它们满足条件 a1^2 + b1^2 == a2^2 + b2^2。

This is an experiment in necromancy :-)

Most answers compare performance and API differences. They miss the fundamental point that the two operations simply have different semantics.

Your intuition is correct. x.equals(y) is not interchangeable with x.compareTo(y) == 0.
The first compares identity, while the other compares the notion of 'size'. It is true that in many cases, especially with primitive types, these two co-align.

The general case is this:

If x and y are identical, they share the same 'size': if x.equals(y) is true => x.compareTo(y) is 0.

However, if x and y share the same size, it does not mean they are identical.

if x.compareTo(y) is 0 does not necessarily mean x.equals(y) is true.

A compelling example where identity differs from size would be complex numbers. Assume that the comparison is done by their absolute value. So given two complex numbers: Z1 = a1 + b1*i and Z2 = a2 + b2*i:

Z1.equals(z2) returns true if and only if a1 = a2 and b1 = b2.

However Z1.compareTo(Z2) returns 0 for and infinite number of (a1,b1) and (a2,b2) pairs as long as they satisfy the condition a1^2 + b1^2 == a2^2 + b2^2.

隱形的亼 2024-08-14 04:18:09

Equals 比 CompareTo 更高效。

如果字符串中的字符序列的长度不匹配,则字符串不可能相等,因此拒绝会更快。

而且,如果是同一个对象(身份相等而不是逻辑相等),它也会更有效。

如果他们还实现了 hashCode 缓存,那么在 hashCode 不匹配的情况下拒绝不等于的速度可能会更快。

Equals can be more efficient then compareTo.

If the length of the character sequences in String doesn't match there is no way the Strings are equal so rejection can be much faster.

Moreover if it is same object (identity equality rather then logical equality), it will also be more efficient.

If they also implemented hashCode caching it could be even faster to reject non-equals in case their hashCode's doesn't match.

酒浓于脸红 2024-08-14 04:18:09

String.equals() 需要调用 instanceof 运算符,而 compareTo() 则不需要。我的同事注意到 equals() 方法中过多的 instanceof 调用导致性能大幅下降,但我的测试证明compareTo() 只是稍微快一点。

不过,我使用的是 Java 1.6。在其他版本(或其他 JDK 供应商)上,差异可能更大。

该测试对 1000 个元素数组中的每个字符串进行比较,重复 10 次。

String.equals() requires invoking instanceof operator while compareTo() requires not. My colleague has noted large performance drop-down caused by excessive numbers of instanceof calls in equals() method, however my test has proved compareTo() to be only slightly faster.

I was using, however, Java 1.6. On other versions (or other JDK vendors) the difference could be larger.

The test compared each-to-each string in 1000 element arrays, repeated 10 times.

楠木可依 2024-08-14 04:18:09
String s1 = "a";
String s2 = "c";

System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

这会打印 -2 和 false

String s1 = "c";
String s2 = "a";
System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

这会打印 2 和 false

String s1 = "c";
String s2 = "c";
System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

这会打印 0 和 true

equals 当且仅当两个字符串匹配时才返回布尔值。

compareTo 不仅可以判断它们是否匹配,还可以判断哪个字符串比另一个字符串小,以及按字典顺序小多少。这主要在集合排序时使用。

String s1 = "a";
String s2 = "c";

System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

This prints -2 and false

String s1 = "c";
String s2 = "a";
System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

This prints 2 and false

String s1 = "c";
String s2 = "c";
System.out.println(s1.compareTo(s2));
System.out.println(s1.equals(s2));

This prints 0 and true

equals returns boolean if and only if both strings match.

compareTo is meant to not just tell if they match but also to tell which String is lesser than the other, and also by how much, lexicographically. This is mostly used while sorting in collection.

哎呦我呸! 2024-08-14 04:18:09

等于 -

1- 重写 GetHashCode 方法以允许类型在哈希表中正确工作。

2- 在 Equals 方法的实现中不要抛出异常。相反,对于空参数返回 false。

3-

  x.Equals(x) returns true.

  x.Equals(y) returns the same value as y.Equals(x).

  (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true.

只要 x 和 y 引用的对象未被修改,连续调用 x.Equals(y) 就会返回相同的值。

x.Equals(null) returns false.

4- 对于某些类型的对象,最好使用 Equals 测试值相等而不是引用相等。如果两个对象具有相同的值,即使它们不是同一个实例,Equals 的此类实现也会返回 true。

例如 -

   Object obj1 = new Object();
   Object obj2 = new Object();
   Console.WriteLine(obj1.Equals(obj2));
   obj1 = obj2; 
   Console.WriteLine(obj1.Equals(obj2)); 

输出:-

False
True

while compareTo -

将当前实例与相同类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例是否在排序中的相同位置之前、之后或出现作为另一个对象的顺序。

它返回 -

小于零 - 该实例在排序顺序中位于 obj 之前。零 - 该实例在排序顺序中与 obj 出现在相同的位置。大于零 - 该实例按排序顺序位于 obj 之后。

如果对象与实例的类型不同,它可能会抛出 ArgumentException。

例如,您可以访问这里。

所以我建议最好使用 Equals 代替 CompareTo。

Equals -

1- Override the GetHashCode method to allow a type to work correctly in a hash table.

2- Do not throw an exception in the implementation of an Equals method. Instead, return false for a null argument.

3-

  x.Equals(x) returns true.

  x.Equals(y) returns the same value as y.Equals(x).

  (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true.

Successive invocations of x.Equals(y) return the same value as long as the object referenced by x and y are not modified.

x.Equals(null) returns false.

4- For some kinds of objects, it is desirable to have Equals test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same value, even if they are not the same instance.

For Example -

   Object obj1 = new Object();
   Object obj2 = new Object();
   Console.WriteLine(obj1.Equals(obj2));
   obj1 = obj2; 
   Console.WriteLine(obj1.Equals(obj2)); 

Output :-

False
True

while compareTo -

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

It returns -

Less than zero - This instance precedes obj in the sort order. Zero - This instance occurs in the same position in the sort order as obj. Greater than zero - This instance follows obj in the sort order.

It can throw ArgumentException if object is not the same type as instance.

For example you can visit here.

So I suggest better to use Equals in place of compareTo.

蹲在坟头点根烟 2024-08-14 04:18:09
  • 等于
    检查相等性和限制重复所需。 Java 库的许多类都使用它来查找重复项。例如 HashSet.add(ob1) 仅当不存在时才会添加。因此,如果您要扩展一些这样的类,请重写 equals()

  • 比较
    订购元素所需的。同样,为了稳定排序,您需要相等,因此返回 0。

  • equals:
    required for checking equality and restricting duplicates. Many classes of Java Library use this in case they wanted to find duplicates. e.g. HashSet.add(ob1) will only add if that doesn't exist. So if you are extending some classes like this then override equals().

  • compareTo:
    required for ordering of element. Again for stable sorting you require equality, so there is a return 0.

蘑菇王子 2024-08-14 04:18:09

“等于”比较对象并返回 true 或 false 和
如果为 true 则“比较”返回 0 或一个数字 [> 0]或[< 0] 如果为假
这是一个例子:

<!-- language: lang-java -->
//Objects Integer
Integer num1 = 1;
Integer num2 = 1;
//equal
System.out.println(num1.equals(num2));
System.out.println(num1.compareTo(num2));
//New Value
num2 = 3;//set value
//diferent
System.out.println(num1.equals(num2));
System.out.println(num1.compareTo(num2));

结果:

num1.equals(num2) =true
num1.compareTo(num2) =0
num1.equals(num2) =false
num1.compareTo(num2) =-1

文档比较:https://docs.oracle.com /javase/7/docs/api/java/lang/Comparable.html

文档等于:https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang .对象)

"equals" compare objects and return true or false and
"compare to" return 0 if is true or an number [> 0] or [< 0] if is false
here an example:

<!-- language: lang-java -->
//Objects Integer
Integer num1 = 1;
Integer num2 = 1;
//equal
System.out.println(num1.equals(num2));
System.out.println(num1.compareTo(num2));
//New Value
num2 = 3;//set value
//diferent
System.out.println(num1.equals(num2));
System.out.println(num1.compareTo(num2));

Results:

num1.equals(num2) =true
num1.compareTo(num2) =0
num1.equals(num2) =false
num1.compareTo(num2) =-1

Documentation Compare to: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Documentation Equals : https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

十秒萌定你 2024-08-14 04:18:09

在使用 compareTo() 而不是 equals() 时,有一点很重要,即 compareTo 适用于实现“Comparable”接口的类,否则它会抛出 NullPointerException 。 String 类实现了 Comparable 接口,而 StringBuffer 则没有,因此您可以在 String< 中使用 "foo".compareTo("doo") /code> 对象,但不在 StringBuffer 对象中。

Here one thing is important while using compareTo() over equals() that compareTo works for the classes that implements 'Comparable' interface otherwise it will throw a NullPointerException. String classes implements Comparable interface while StringBuffer does not hence you can use "foo".compareTo("doo") in String object but not in StringBuffer Object.

风透绣罗衣 2024-08-14 04:18:09

我相信 StringequalsequalsIgnoreCase 方法返回 truefalse 这是有用的如果你想比较字符串对象的值,但在实现compareTo和compareToIgnoreCase方法的情况下返回正值、负值和零值,这在排序时很有用。

I believe equals and equalsIgnoreCase methods of String return true and false which is useful if you wanted to compare the values of the string object, But in case of implementing compareTo and compareToIgnoreCase methods returns positive, negative and zero value which will be useful in case of sorting.

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