Comparing two byte arrays.

I need to know if one byte array is equal to another. Is there any method to compare two byte arrays? I know one way by comparing byte to byte, but if arrays are big, it's very slow.Thanks.
[211 byte] By [jperezsanz] at [2007-9-26 7:30:19]
# 1

> I need to know if one byte array is equal to another. Is there any method to compare two byte arrays?

byte[] barray1 = new byte[10];

byte[] barray2 = new byte[10];

//fill the arrays

boolean arraysEqual = java.util.Arrays.equals(barray1, barray2);

> I know one way by comparing byte to byte, but if arrays are

> big, it's very slow.

>

> Thanks.

How else could you compare the 2 arrays? I'm sure that this Sun implementation has been tuned for speed

oxbow_lakes at 2007-7-1 17:27:25 > top of Java-index,Core,Core APIs...
# 2

If you know nothing about the arrays, then it is possible that they are identical except for one entry. So it follows that any algorithm to compare them for equality must be prepared to look at every entry in the array. No good moaning that it's slow, it's as fast as possible.

But if you DO know something about the arrays, perhaps that the data in them is structured in a certain way, then it might be possible to write more complicated but faster comparison algorithms.

DrClap at 2007-7-1 17:27:25 > top of Java-index,Core,Core APIs...