Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个非常长的字符串是否相等_Java_Android_Algorithm_Performance - Fatal编程技术网

Java 比较两个非常长的字符串是否相等

Java 比较两个非常长的字符串是否相等,java,android,algorithm,performance,Java,Android,Algorithm,Performance,我知道在java中,我们可以使用.equals方法来比较两个字符串是否相等。但是如果这两个字符串非常长,例如: String s1 = "t was her sister Josephine who told her, in broken sentences; veiled hints that revealed in half concealing. Her husband's friend Richards was there, too, near her. It was he who ha

我知道在java中,我们可以使用.equals方法来比较两个字符串是否相等。但是如果这两个字符串非常长,例如:

String s1 = "t was her sister Josephine who told her, in broken sentences; veiled hints that revealed in half concealing. Her husband's friend Richards was there, too, near her. It was he who had been in the newspaper office when intelligence of the railroad disaster was received, with Brently Mallard's name leading the"

String s2 = "t was her sister Josephine who told her, in broken sentences; veiled hints that revealed in half concealing. Her husband's friend Richards was there, too, near her. It was he who had been in the newspaper office when intelligence of the railroad disaster was received, with Brently Mallard's name leading the"


if s1.equals(s2) return true;

如果要比较的字符串很大,那么.equals仍然有效吗?有限制吗?我需要知道比较两个长字符串的最佳方法是什么。

是的,可以。可能需要一些时间(很可能一秒钟也不会)。但不要低估现代设备的编译能力。它将在一眨眼之间进行比较

如果要比较的字符串很大,那么.equals仍然有效吗

有限制吗

只有Java字符串的最大大小(2^31-1个字符)或堆大小施加的限制

(但这些是对字符串本身大小的限制,而不是对
equals
方法的限制。)


我需要知道比较两个长字符串的最佳方法是什么

使用
String.equals(对象)


如果您有多个字符串和/或希望对其进行多次比较,则将
String.hashCode()
String.equals
结合使用可以提高性能。(这取决于要比较的字符串的性质,即成对相等与“几乎”相等的概率。)

当您尝试它时发生了什么?是的,它会起作用,您给出的句子对它来说根本不长。有任何限制吗?对若字符串长于3个字符,则等于返回随机值。。。认真地String.equals定义得很好。。。字符串长度也有限制。。。只需添加1+1,除非您为非常非常小的嵌入式设备开发。。。你可能应该试着获得一剂好的“现实感”。你似乎非常依赖于对现实的假设。我的意思是:有一个运行在JVM上的LMAX系统,其内部处理请求的平均延迟为100微秒。这个系统每秒处理成吨的请求。换言之:上述比较在2016年并不昂贵;绝对不是。但是equals方法有什么限制吗?我需要知道比较两个长字符串的最佳方法是什么。你可以安全地使用equals。“可能”、“某个时间”、“最有可能”、“甚至一秒钟”、“眨眼之间”。。。这是如此模糊,我不确定它是否应该作为一个答案发布。我认为一个评论而不是否决票更有可能导致一个更好的答案。无意冒犯。(有些人甚至希望在否决投票时强制发表评论;请参阅)