Question: I have a byte array:

```
private final static byte[][] ARRAY = {
    { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd },
    { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 }
};

```

Given an arbitrary byte array:

```
byte[] check = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44) };

```

or

```
byte[] check2 = { (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45) };

```

What's the best way to check if either `check` or `check2` is in `ARRAY` exactly as written (in the same order etc)?

I can change `ARRAY` to any other data structure as needed but `check` and `check2` are provided as byte arrays.

Answer: 
You can use `Arrays.equals()` to compare two arrays. From the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#equals(byte[],%20byte[])):

> 
> Returns true if the two specified arrays of bytes are equal to one
>  another. Two arrays are considered equal if both arrays contain the
>  same number of elements, and all corresponding pairs of elements in
>  the two arrays are equal.
> 
> 
> 

```
byte[] check = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 };
byte[] check2 = { (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45 };

int i = 0;
for (; i < ARRAY.length; i++) {
    if (Arrays.equals(ARRAY[i], check)) {
        System.out.println("check[] found at index: " + i);
        break;
    }
}
if (i == ARRAY.length) {
    System.out.println("check[] not found");
}

for (i = 0; i < ARRAY.length; i++) {
    if (Arrays.equals(ARRAY[i], check2)) {
        System.out.println("check2[] found at index: " + i);
        break;
    }
}
if (i == ARRAY.length) {
    System.out.println("check2[] not found");
}

```

***Output :***

```
check[] found at index: 1
check2[] not found

```
Use a byte[] wrapper class and HashSet.

In the wrapper class override `equals()` and `hashCode()` using `Arrays.equals(byte[],byte[])` and `Arrays.hashCode(byte[])` ,then HashSet will match other byte array which has the exactly the same elements.

like this:

```
public static void main(String[] args) {
    Set<MyArray> ARRAY_SET = new HashSet<MyArray>();
    ARRAY_SET.add(new MyArray(new byte[] { (byte) 0xaa, (byte) 0xbb,
            (byte) 0xcc, (byte) 0xdd }));
    ARRAY_SET.add(new MyArray(new byte[] { (byte) 0x11, (byte) 0x22,
            (byte) 0x33, (byte) 0x44 }));
    byte[] check = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 };
    byte[] check2 = { (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45 };
    System.out.println(ARRAY_SET.contains(new MyArray(check)));//true
    System.out.println(ARRAY_SET.contains(new MyArray(check2)));//false
}

static class MyArray {
    private byte[] array;

    public MyArray(byte[] array) {
        super();
        this.array = array;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(array);
        return result;
    }

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

```