本文介绍了比较Java中字符串的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较Java中两个字符串最快的是什么?

What's the quickest to compare two strings in Java?

是否有比等于更快的东西?

Is there something faster than equals?

编辑:
我无法澄清这个问题。

I can not help much to clarify the problem.

我有两个按字母顺序排序并完全相同的字符串

I have two String which are sorted alphabetically and EXACTLY the same size

示例:abbcee和abcdee

Example: abbcee and abcdee

字符串最长可达30个字符

Strings can be long up to 30 characters

推荐答案

我不希望 Oracle尚未优化标准 String#equals()到最大所以,我希望它已经是最快捷的方式了。如果你想了解它们是如何实现的,请在它的源代码中查看一下。这是一个摘录:

I don't expect that Oracle hasn't already optimized the standard String#equals() to the max. So, I expect it to be already the quickest way. Peek a bit round in its source if you want to learn how they implemented it. Here's an extract:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

这篇关于比较Java中字符串的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:28