arrays.sort(._Arrays.hashCode(Object [])与Objects.hash(Object…)
arrays.sort(.
從JDK 1.5開始 , Arrays類提供了名為“ hashCode ”的重載static方法。 大多數(shù)重載方法都接受特定原始類型的數(shù)組,但是Arrays.hashCode(Object [])方法可用于計(jì)算引用類型數(shù)組的int哈希碼。 自從JDK 1.7誕生以來 , Objects類提供了一種名為hash(Object…)的方法,該方法還為提供的Java對象數(shù)組(表示Java varargs的省略號 [ ... ] 作為數(shù)組處理 )返回int哈希碼。 接受一個(gè)數(shù)組 )。 這篇文章提供了Arrays.hashCode(Object)和Objects.hash(Object...)之間的簡要比較。
我們可以查看OpenJDK中的代碼,以了解OpenJDK如何實(shí)現(xiàn)此處比較的兩種方法。 事實(shí)證明Arrays.hashCode(Object[])和Objects.hash(Object...)行為完全相同,因?yàn)镺bjects.hash(Object...)完全委托給Arrays.hashCode(Object[]) 。 這是從OpenJDK Objects.java類提取的下一個(gè)代碼清單中顯示的。
public static int hash(Object... values) {return Arrays.hashCode(values); }因此,事實(shí)證明這些方法實(shí)際上是相同的,因此選擇哪種方法主要取決于口味。 鑒于某些情況,直接使用Arrays方法可能會吸引一些人。 其他人可能更喜歡在將已知的Java數(shù)組構(gòu)造傳遞給Arrays方法時(shí)使用Objects方法,而在以逗號分隔的組合形式傳遞值而無需顯式數(shù)組語法的情況下使用Objects方法(例如(例如,實(shí)現(xiàn)自定義類的hashCode()方法并將該類的任意類型的屬性傳遞給哈希代碼計(jì)算的情況)。 當(dāng)使用相同類型的原語數(shù)組時(shí),最好為該特定原語使用適當(dāng)版本的Arrays.hashCode 。
下一個(gè)代碼清單(可在GitHub上找到)中顯示的簡單類演示了Arrays.hashCode和Objects.hash(Object...)方法的重載版本之間在輸出方面的異同。
package dustin.examples.hashcodes;import java.util.Arrays; import java.util.Objects;import static java.lang.System.out;/*** Demonstration that displays output to standard output with* hash codes generated for the same underlying array data by* both {@code Arrays.hashCode(Object[])} and by* {@code Objects.hash(Object...)}.*/ public class HashesComparedDemo {public static void main(final String[] arguments){final int[] integers = ArraysCreator.createArrayOfInts();out.println("Arrays.hashCode(Object[]) for int[]: " + Arrays.hashCode(integers));out.println("Objects.hash(Object...) for int[]: " + Objects.hash(integers));out.println("Objects.hashCode(Object) for int[]: " + Objects.hashCode(integers));final Integer[] refIntegers = ArraysCreator.createArrayOfIntegers();out.println("Arrays.hashCode(Object[]) for Integer[]: " + Arrays.hashCode(refIntegers));out.println("Objects.hash(Object...) for Integer[]: " + Objects.hash(refIntegers));out.println("Objects.hashCode(Object) for Integer[]: " + Objects.hashCode(refIntegers));final String[] strings = ArraysCreator.createArrayOfStrings();out.println("Arrays.hashCode(Object[]) for String[]: " + Arrays.hashCode(strings));out.println("Objects.hash(Object...) for String[]: " + Objects.hash(strings));out.println("Objects.hashCode(Object) for String[]: " + Objects.hashCode(strings));} }上面顯示的代碼將三個(gè)通用數(shù)據(jù)集(原始int值數(shù)組,參考Integer值數(shù)組和String值數(shù)組)傳遞給Arrays.hashCode , Objects.hash(Object...)和Objects.hashCode(Object)方法,該方法接受單個(gè)Object (整個(gè)數(shù)組符合該條件)。 然后,簡單示例將每種方法為每個(gè)數(shù)據(jù)集生成的各個(gè)哈希碼值寫入標(biāo)準(zhǔn)輸出。 接下來顯示運(yùn)行此代碼的結(jié)果。
Arrays.hashCode(Object[]) for int[]: 1722319241 Objects.hash(Object...) for int[]: 356573628 Objects.hashCode(Object) for int[]: 356573597 Arrays.hashCode(Object[]) for Integer[]: 1722319241 Objects.hash(Object...) for Integer[]: 1722319241 Objects.hashCode(Object) for Integer[]: 1735600054 Arrays.hashCode(Object[]) for String[]: 448603921 Objects.hash(Object...) for String[]: 448603921 Objects.hashCode(Object) for String[]: 21685669如我們所料, Arrays.hashCode(Object[])和Objects.hash(Object...)對于引用類型Integer和String返回相同的計(jì)算哈希碼,因?yàn)樗鼈儍烧邔?shí)際上都是Arrays.hashCode(Object[]) 。 原始int值數(shù)組從Arrays.hashCode(int[])得出的結(jié)果與從Objects.hash(Object...) ,這當(dāng)然是因?yàn)樵紨?shù)組被傳遞給重載的Arrays.hashCode(int[])方法專門針對該原始數(shù)據(jù)類型而不是Arrays.hashCode(Object[]) 。
翻譯自: https://www.javacodegeeks.com/2018/09/arrays-hashcodeobject-versus-objects-hashobject.html
arrays.sort(.
總結(jié)
以上是生活随笔為你收集整理的arrays.sort(._Arrays.hashCode(Object [])与Objects.hash(Object…)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 古代县令相当于现在什么官职(县令在古代是
- 下一篇: feign rest_与Feign客户轻