Java String类(JDK1.8)

2017-03-21  本文已影响21人  LinDelta

一、hashCode函数

public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }

public static int calculate(int radix, int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum * radix + a[i]; } return sum; }

二、equals函数

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

上一篇 下一篇

猜你喜欢

热点阅读