Home  >  Article  >  Using byte arrays as Map keys

Using byte arrays as Map keys

王林
王林forward
2024-02-08 23:51:301014browse

In PHP, using byte arrays as Map keys is an efficient method that can improve program performance and memory utilization. Byte arrays as keys have faster access speeds and less memory consumption than strings as keys. By using byte arrays as keys, you can effectively save memory space, especially when dealing with large amounts of data. In addition, byte arrays can also provide more flexible key name operations, such as size comparison, interception and splicing, etc. All in all, using byte arrays as Map keys is a useful technique in PHP development that can improve program performance and efficiency.

Question content

Do you find any problems with using byte arrays as Map keys? I could also do new String(byte[]) and hash via String, but using byte[] is more straightforward.

Solution

The problem is that byte[] uses object identifiers for equals and hashcode, so

byte[] b1 = {1, 2, 3}
byte[] b2 = {1, 2, 3}

Does not match in hashmap. I see three options:

  1. Wrapped in string, but you have to be careful about encoding issues (you need to make sure byte -> string -> byte gives you the same bytes).
  2. Use listdf12804343f5520ff3af5d9f86b34817 (large memory consumption).
  3. Write your own wrapper class and write hashcode and equals to use the contents of the byte array.

As long as you only want the references of the keys to be equal, that's fine - arrays don't implement "value equality" in the way you might want. For example:

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());

Print something similar:

false
1671711
11394033

(The actual numbers are irrelevant; the fact that they are different is important.)

Assuming you actually want equality, I suggest you create your own wrapper that takes byte[] and implements equality and hash code generation appropriately:

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);
    }
}

Please note that if you change the value in the byte array after using bytearraywrapper as a key in hashmap (etc.), you will encounter The problem... you can use the bytearraywrapper constructor if you want, but obviously this is a waste of performance if you know you won't change the contents of the byte array.

Edit: As mentioned in the comments, you can also use bytebuffer (specifically its bytebuffer#wrap(byte[]) method) . I don't know if this is really the right thing to do since bytebuffers have all the extra features you don't need, but it is an option.

The above is the detailed content of Using byte arrays as Map keys. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:RESTful trigger functionNext article:RESTful trigger function