how to compare the Java Byte[] array?

Java

Java Problem Overview


public class ByteArr {

    public static void main(String[] args){
        Byte[] a = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
        Byte[] b = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
        byte[] aa = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
        byte[] bb = {(byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00};
	
        System.out.println(a);
        System.out.println(b);
        System.out.println(a == b);
        System.out.println(a.equals(b));
	
        System.out.println(aa);
        System.out.println(bb);
        System.out.println(aa == bb);
        System.out.println(aa.equals(bb));
    }
}

I do not know why all of them print false.

When I run "java ByteArray", the answer is "false false false false".

I think the a[] equals b[] but the JVM is telling me I am wrong, why??

Java Solutions


Solution 1 - Java

Use Arrays.equals() if you want to compare the actual content of arrays that contain primitive types values (like byte).

System.out.println(Arrays.equals(aa, bb));

Use Arrays.deepEquals for comparison of arrays that contain objects.

Solution 2 - Java

Cause they're not equal, ie: they're different arrays with equal elements inside.

Try using Arrays.equals() or Arrays.deepEquals().

Solution 3 - Java

As byte[] is mutable it is treated as only being .equals() if its the same object.

If you want to compare the contents you have to use Arrays.equals(a, b)

BTW: Its not the way I would design it. ;)

Solution 4 - Java

have you looked at Arrays.equals()?

Edit: if, as per your comment, the issue is using a byte array as a HashMap key then see this question.

Solution 5 - Java

If you're trying to use the array as a generic HashMap key, that's not going to work. Consider creating a custom wrapper object that holds the array, and whose equals(...) and hashcode(...) method returns the results from the java.util.Arrays methods. For example...

import java.util.Arrays;

public class MyByteArray {
   private byte[] data;

   // ... constructors, getters methods, setter methods, etc...
   

   @Override
   public int hashCode() {
      return Arrays.hashCode(data);
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      MyByteArray other = (MyByteArray) obj;
      if (!Arrays.equals(data, other.data))
         return false;
      return true;
   }
   
   
}

Objects of this wrapper class will work fine as a key for your HashMap<MyByteArray, OtherType> and will allow for clean use of equals(...) and hashCode(...) methods.

Solution 6 - Java

Because neither == nor the equals() method of the array compare the contents; both only evaluate object identity (== always does, and equals() is not overwritten, so the version from Object is being used).

For comparing the contents, use Arrays.equals().

Solution 7 - Java

They are returning false because you are testing for object identity rather than value equality. This returns false because your arrays are actually different objects in memory.

If you want to test for value equality should use the handy comparison functions in java.util.Arrays

e.g.

import java.util.Arrays;

'''''

Arrays.equals(a,b);

Solution 8 - Java

Try for this:

boolean blnResult = Arrays.equals(byteArray1, byteArray2);

I am also not sure about this, but try this may be it works.

Solution 9 - Java

You can also use a ByteArrayComparator from Apache Directory. In addition to equals it lets you compare if one array is greater than the other.

Solution 10 - Java

why a[] doesn't equals b[]? Because equals function really called on Byte[] or byte[] is Object.equals(Object obj). This functions only compares object identify , don't compare the contents of the array.

Solution 11 - Java

I looked for an array wrapper which makes it comparable to use with guava TreeRangeMap. The class doesn't accept comparator.

After some research I realized that ByteBuffer from JDK has this feature and it doesn't copy original array which is good. More over you can compare faster with ByteBuffer::asLongBuffer 8 bytes at time (also doesn't copy). By default ByteBuffer::wrap(byte[]) use BigEndian so order relation is the same as comparing individual bytes.

.

Solution 12 - Java

Java byte compare,

public static boolean equals(byte[] a, byte[] a2) {
		if (a == a2)
			return true;
		if (a == null || a2 == null)
			return false;

		int length = a.length;
		if (a2.length != length)
			return false;

		for (int i = 0; i < length; i++)
			if (a[i] != a2[i])
				return false;

		return true;
	}

Solution 13 - Java

You can also use org.apache.commons.lang.ArrayUtils.isEquals()

Solution 14 - Java

Arrays.equals is not enough for a comparator, you can not check the map contain the data. I copy the code from Arrays.equals, modified to build a Comparator.

class ByteArrays{
    public static <T> SortedMap<byte[], T> newByteArrayMap() {
        return new TreeMap<>(new ByteArrayComparator());
    }

    public static SortedSet<byte[]> newByteArraySet() {
        return new TreeSet<>(new ByteArrayComparator());
    }

    static class ByteArrayComparator implements Comparator<byte[]> {
        @Override
        public int compare(byte[] a, byte[] b) {
            if (a == b) {
                return 0;
            }
            if (a == null || b == null) {
                throw new NullPointerException();
            }

            int length = a.length;
            int cmp;
            if ((cmp = Integer.compare(length, b.length)) != 0) {
                return cmp;
            }

            for (int i = 0; i < length; i++) {
                if ((cmp = Byte.compare(a[i], b[i])) != 0) {
                    return cmp;
                }
            }

            return 0;
        }
    }
}

Solution 15 - Java

There's a faster way to do that:

Arrays.hashCode(arr1) == Arrays.hashCode(arr2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLazyView Question on Stackoverflow
Solution 1 - JavaLukaszView Answer on Stackoverflow
Solution 2 - JavasoulcheckView Answer on Stackoverflow
Solution 3 - JavaPeter LawreyView Answer on Stackoverflow
Solution 4 - JavaDan VintonView Answer on Stackoverflow
Solution 5 - JavaHovercraft Full Of EelsView Answer on Stackoverflow
Solution 6 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 7 - JavamikeraView Answer on Stackoverflow
Solution 8 - JavaBhavesh ShahView Answer on Stackoverflow
Solution 9 - JavaAndrejsView Answer on Stackoverflow
Solution 10 - JavaJack47View Answer on Stackoverflow
Solution 11 - JavaDaniil IaitskovView Answer on Stackoverflow
Solution 12 - JavaRakesh ChaudhariView Answer on Stackoverflow
Solution 13 - JavaRoutesMaps.comView Answer on Stackoverflow
Solution 14 - JavawenerView Answer on Stackoverflow
Solution 15 - JavanagyatkaView Answer on Stackoverflow