Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's okay so long as you only want reference equality for your key - arrays don't implement "value equality" in the way that you'd probably want. For example:</p> <pre><code>byte[] array1 = new byte[1]; byte[] array2 = new byte[1]; System.out.println(array1.equals(array2)); System.out.println(array1.hashCode()); System.out.println(array2.hashCode()); </code></pre> <p>prints something like:</p> <pre><code>false 1671711 11394033 </code></pre> <p>(The actual numbers are irrelevant; the fact that they're different is important.)</p> <p>Assuming you <em>actually</em> want equality, I suggest you create your own wrapper which contains a <code>byte[]</code> and implements equality and hash code generation appropriately:</p> <pre><code>public final class ByteArrayWrapper { private final byte[] data; public ByteArrayWrapper(byte[] data) { if (data == null) { throw new NullPointerException(); } this.data = data; } @Override public boolean equals(Object other) { if (!(other instanceof ByteArrayWrapper)) { return false; } return Arrays.equals(data, ((ByteArrayWrapper)other).data); } @Override public int hashCode() { return Arrays.hashCode(data); } } </code></pre> <p>Note that if you change the values within the byte array after using the <code>ByteArrayWrapper</code>, as a key in a <code>HashMap</code> (etc) you'll have problems looking up the key again... you could take a copy of the data in the <code>ByteArrayWrapper</code> constructor if you want, but obviously that will be a waste of performance if you know you <em>won't</em> be changing the contents of the byte array.</p> <p>EDIT: As mentioned in the comments, you could also use <code>ByteBuffer</code> for this (in particular, its <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#wrap%28byte%5B%5D%29" rel="noreferrer"><code>ByteBuffer#wrap(byte[])</code></a> method). I don't know whether it's really the right thing, given all the extra abilities that <code>ByteBuffer</code>s have which you don't need, but it's an option.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload