日期:2014-05-20 浏览次数:20755 次
public boolean contentEquals(CharSequence cs) { if (count != cs.length()) return false; // Argument is a StringBuffer, StringBuilder if (cs instanceof AbstractStringBuilder) { char v1[] = value; char v2[] = ((AbstractStringBuilder)cs).getValue(); int i = offset; int j = 0; int n = count; while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } ///////////// SINCE ALL CHARACTERS ARE EQUAL, WHY NOT RETURN TRUE HERE? } // Argument is a String if (cs.equals(this)) return true; // Argument is a generic CharSequence char v1[] = value; int i = offset; int j = 0; int n = count; while (n-- != 0) { if (v1[i++] != cs.charAt(j++)) return false; } return true; }
------解决方案--------------------
比如,"abc".contentEquals(new StringBuilder("abc"))将比较两次
------解决方案--------------------
String中间另外一个问题(也在7中修正),虽然谈不上性能影响,但是很傻:
private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 8575799808933029326L; public int compare(String s1, String s2) { int n1=s1.length(), n2=s2.length(); for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) { char c1 = s1.charAt(i1); char c2 = s2.charAt(i2); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { return c1 - c2; } } } } return n1 - n2; } }
------解决方案--------------------
类a在sun的jvm可能很慢,但在ibm j9或者jrockit上可能很快,这个说不好的,再说有运行时优化,会编译成本地代码,不同的jvm优化算法也是大不相同,自然效率也就不一样
------解决方案--------------------
我知道有几个: