Aus irgendeinem Grund kann Arrays.deepHashCode()
nicht mit byte[]
arbeiten.
Gibt es ein anderes Äquivalent?deepHashCode mit Byte-Array
Antwort
Zunächst einmal, keine Notwendigkeit für "Deep". Es ist ein Primitiv. Du brauchst kein Deep.
Verwenden Sie einfach Arrays.hashCode(byte[] yourArray)
Edit: Um zu klären, impliziert tief in die in der Anordnung enthalten Objekte einzutauchen. Wenn Sie mit einem Primitiv arbeiten, müssen Sie nur den Primitivwert selbst in der Berechnung verwenden. Deshalb dreht sich keine der Deep-Methoden um Primitive.
Hinweis: Für alle Neugierigen, fügte ich ein Beispiel für deepHashCode für verschachtelte Arrays in einer separaten Antwort –
Mit deepHashCode ist in der Tat richtig, wenn Sie zwei Byte-Arrays wollen die gleichen Bytes enthalten äquivalente Hash-Codes zu haben, müssen Sie nur einige zusätzliche Casting für die byte [] Array.
import java.utils.Arrays;
public class A {
public static void main(String[] args) {
byte[] a = {10,32,-43,80};
byte[] b = {13,-40};
byte[] c = {10,32,-43,80};
// A and C will have different hash codes
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(c.hashCode());
// A and C will now have equivalent hash codes
System.out.println(Arrays.deepHashCode(new Object[]{a}));
System.out.println(Arrays.deepHashCode(new Object[]{b}));
System.out.println(Arrays.deepHashCode(new Object[]{c}));
}
}
Daraus ergibt sich eine Ausgabe ähnlich ...
// Hash Codes
a = 16130931
b = 26315233
c = 32716405
// Deep hash codes
a = 1250961
b = 1355
c = 1250961
Sehr nette Vorgehensweise zum Hinzufügen von ausführbaren Code in Ihrer Antwort =) Für primitive Arrays; Allerdings ist Arrays.hashCode besser geeignet als Arrays.eepHashCode. Ich habe Ihren Code modifiziert und eine separate Antwort hinzugefügt, die zeigt, dass er immer noch identische hashCodes mit der gegebenen Eingabe bereitstellt, sowie ein Beispiel, wenn sich die beiden unterscheiden. –
Die akzeptierte Antwort ist richtig: mit Arrays.hashCode gibt identische Ergebnisse für byte [] mit den gleichen Werten. Arrays.deepHashCode ist notwendig, wenn Sie eine verschachtelte (tiefe) Struktur haben.
import java.util.Arrays;
public class A {
public static void main(String[] args) {
byte[] a = {10, 32, -43, 80};
byte[] b = {13, -40};
byte[] c = {10, 32, -43, 80};
System.out.println("NOTE: A and C have identical values, B differs");
System.out.println("Using byte[].hashCode(): A and C have different hash codes");
System.out.println("a = " + a.hashCode());
System.out.println("b = " + b.hashCode());
System.out.println("c = " + c.hashCode());
System.out.println("Using Arrays.hashCode(): A and C have identical hash codes");
System.out.println("a = " + Arrays.hashCode(a));
System.out.println("b = " + Arrays.hashCode(b));
System.out.println("c = " + Arrays.hashCode(c));
System.out.println("Using Arrays.deepHashCode(): A and C have identical hash codes");
System.out.println("a = " + Arrays.deepHashCode(new Object[]{a}));
System.out.println("b = " + Arrays.deepHashCode(new Object[]{b}));
System.out.println("c = " + Arrays.deepHashCode(new Object[]{c}));
}
}
Dies resultiert in der Ausgabe:
NOTE: A and C have identical values, B differs
Using byte[].hashCode(): A and C have different hash codes
a = 141847843
b = 329849131
c = 1119051810
Using Arrays.hashCode(): A and C have identical hash codes
a = 1250930
b = 1324
c = 1250930
Using Arrays.deepHashCode(): A and C have identical hash codes
a = 1250961
b = 1355
c = 1250961
Hier ist ein Beispiel dafür, wann Arrays.deepHashCode
import java.util.Arrays;
public class B {
public static void main(String[] args) {
Object[] d = {"abc", "def", new String[]{"ghi"}};
Object[] e = {"abc", "def", new String[]{"ghi"}};
System.out.println("NOTE: D and E have identical nested values");
System.out.println("Using Object[].hashCode(): different");
System.out.println("d = " + d.hashCode());
System.out.println("f = " + e.hashCode());
System.out.println("Using Arrays.hashCode(): still different");
System.out.println("d = " + Arrays.hashCode(d));
System.out.println("e = " + Arrays.hashCode(e));
System.out.println("Using Arrays.deepHashCode(): identical");
System.out.println("d = " + Arrays.deepHashCode(d));
System.out.println("e = " + Arrays.deepHashCode(e));
}
}
notwendig ist
Ausgabe:
NOTE: D and E have identical nested values
Using Object[].hashCode(): different
d = 241990244
f = 1943487137
Using Arrays.hashCode(): still different
d = 1057745997
e = 709187068
Using Arrays.deepHashCode(): identical
d = 95807651
e = 95807651
- 1. mit Bytearray mit socket.recv_into
- 2. Datatable.select mit Bytearray VB.NET
- 3. `struct.unpack_from` funktioniert nicht mit` bytearray`?
- 4. Hex byteArray
- 5. Datei aus Bytearray öffnen
- 6. Bytearray zu Bild asp.net
- 7. Wie konvertiert man Bytearray zu Bild oder Bild zu Bytearray?
- 8. Python byteArray Printing
- 9. VB.Net ByteArray Speicherleck
- 10. Bytearray von einer Webcam
- 11. Hbase byteArray nach Long
- 12. .NET Encrypt Bytearray
- 13. wie zu response.write bytearray?
- 14. C# ByteArray zu Image
- 15. Sende ByteArray an JavaScript
- 16. von ByteArray zu BigInteger
- 17. C#: Bytearray in XML speichern
- 18. Ärger einen erweiterten ByteArray Klonen
- 19. Python gibt seltsames Bytearray aus
- 20. Java - Größe der Komprimierungsausgabe-ByteArray
- 21. Android: conerting Bild in byteArray
- 22. PIL: Bytearray in Bild konvertieren
- 23. Converting ein Float in Bytearray
- 24. Linux zu Windows C++ Bytearray
- 25. Nummer zu ByteArray (Int64 BigEndian)
- 26. Unterschied zwischen Bytearray und Liste
- 27. Bytearray von Oracle RAW in System.Guid konvertieren?
- 28. Upload ByteArray Bild auf Facebook mit Javascript SDK
- 29. Convert Char in Bytearray mit Hex-Daten 0x31
- 30. "Buchstäblich" einen String in ein Bytearray konvertieren
Sie beziehen t o 'deepHashCode()' in 'java.util.Arrays', nehme ich an? – skaffman