java equals(...) 和 equalsIgnoreCase(...)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2483029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:24:26  来源:igfitidea点击:

equals(...) and equalsIgnoreCase(...)

java

提问by Vaibhav Bajpai

Why do we have equals()and equalsIgnoreCase()as two different methods, when equals()could have been overloaded with a special ignoreCaseargument to provide equalsIgnoreCase()functionality?

为什么我们有equals()equalsIgnoreCase()作为两种不同的方法,何时equals()可以用特殊ignoreCase参数重载以提供equalsIgnoreCase()功能?

回答by Péter T?r?k

The method equals()is inherited from Object, so its signature should not be changed. equals()can often be used without actually knowing the concrete class of the object, e.g. when iterating through a collection of objects (especially before Java 5 generics). So then you wouldn't even see the other equals()without downcasting your object to Stringfirst.

该方法equals()继承自Object,因此不应更改其签名。equals()通常可以在不知道对象的具体类的情况下使用,例如在迭代对象集合时(尤其是在 Java 5 泛型之前)。因此,如果不equals()将对象向下投射到String第一个,您甚至不会看到另一个。

This was a design choice from the creators of Java to make the idiom of using equals()usable exactly the same way for all objects.

这是 Java 创造者的一个设计选择,目的是使使用的习惯用法equals()对所有对象都完全相同。

Moreover, IMO

此外,国际海事组织

if (string1.equalsIgnoreCase(string2)) ...

is more readable, thus less error-prone than

更具可读性,因此比

if (string1.equals(string2, true)) ...

Of course, in your own classes you are free to add an equals()with different signature (on top of the standard equals(), that is).

当然,在您自己的类中,您可以随意添加equals()具有不同签名的(在标准 之上equals())。

回答by Venkat

equalIgnoreCase()is used for ignore the Case sensitive of our String. But the equals()is only returns true, while be same case of string

equalIgnoreCase()用于忽略我们的String. 但equals()它只是返回true,而同样的情况string

ex,

前任,

String value="java";
if(value.equals("JAva")
{
    System.out.println("Return True");
}
else
{
    System.out.println("Return False");
}

Ans: Returns False

答:退货 False

but the other one is,

但另一个是,

if(value.equalIgnoreCase("JAva")
{
    System.out.println("Return True");
}
else
{
    System.out.println("Return False");
}

Ans: Returns True

答:退货 True

回答by missingfaktor

It's absolutely possible to do what you are suggesting but the language designers chose to go the other way and hence we have equalsIgnoreCase(otherString)instead of say equals(otherString, StringConstants.IGNORE_CASE)or equals(otherString, true).

完全有可能按照您的建议去做,但是语言设计者选择了另一种方式,因此我们有equalsIgnoreCase(otherString)而不是 say equals(otherString, StringConstants.IGNORE_CASE)or equals(otherString, true)

回答by Roman

Because equals()method is inherited from Object.

因为equals()方法是从Object继承的。

If they did it as you suggest then we would have something like this:

如果他们按照你的建议去做,那么我们会有这样的事情:

public final class String {

    public boolean equals () { ... }

    public boolean equals (boolean ignoreCase) { ... }

} 

And without reading documentation it would be impossible to understand what method equals()(which without parameter) do.

如果不阅读文档,就不可能理解什么方法equals()(没有参数)做什么。

回答by MauriceL

The main test when overridng a method with additional parameters is that I would expect any method override to do exactly the same thing as the method it's overriding. Equals(), being derived from Object has a contract it must follow. Two objects that are equal() should have identical hashcodes. I don't think two objects that are case insensitive equal should have the same hashcode, so I believe overriding equal here is the wrong thing to do.

使用附加参数覆盖方法时的主要测试是,我希望任何方法覆盖都与它覆盖的方法做完全相同的事情。从 Object 派生的 Equals() 有一个必须遵守的契约。equal() 的两个对象应该具有相同的哈希码。我认为不区分大小写的两个对象不应该具有相同的哈希码,所以我认为在这里覆盖 equal 是错误的做法。

回答by Parth Bhayani

    // Demonstrate equals() and equalsIgnoreCase(). 
    class equalsDemo { 
    public static void main(String args[]) { 
    String s1 = "Hello"; 
    String s2 = "Hello"; 
    String s3 = "Good-bye"; 
    String s4 = "HELLO"; 
    System.out.println(s1 + " equals " + s2 + " -> " + 
    s1.equals(s2)); 
    System.out.println(s1 + " equals " + s3 + " -> " + 
    s1.equals(s3)); 
    System.out.println(s1 + " equals " + s4 + " -> " + 
    s1.equals(s4)); 
    System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + 
    s1.equalsIgnoreCase(s4)); 
    } 
}

The output from the program is shown here:

程序的输出如下所示:

Hello equals Hello -> true 
Hello equals Good-bye -> false 
Hello equals HELLO -> false 
Hello equalsIgnoreCase HELLO -> true

回答by Fakrudeen

I think they just chose one of the alternatives. .NET chose the other. StringComparison.InvariantCultureIgnoreCase etc.

我认为他们只是选择了其中一种选择。.NET 选择了另一个。StringComparison.InvariantCultureIgnoreCase 等。

Definitely what you are suggesting and [even better what] .NET implemented would have been more flexible for different cultures etc. In fact I don't even know what culture they use in this ignore case. I guess Current culture.

绝对是你的建议和[甚至更好的] .NET 实施对于不同的文化等会更灵活。事实上,我什至不知道他们在这种忽略情况下使用什么文化。我猜现在的文化。