简体   繁体   English

进入Hashmap返回null

[英]get in Hashmap returns null

I have a basic Hashmap code that wont work for some reason. 我有一个基本的Hashmap代码,由于某些原因该代码无法正常工作。

ConcurrentHashMap<Bitmap, byte[]> pixels = new ConcurrentHashMap<Bitmap, byte[]>();

it key is bitmap, and the value are its bytes that I get using this code: 它的关键是位图,值是我使用以下代码得到的字节:

public byte[] getPixels(Bitmap bmp) {
    int bytes = bmp.getRowBytes() * bmp.getHeight();
 buffer = ByteBuffer.allocateDirect(bytes);
    bmp.copyPixelsToBuffer(buffer);
    buffer.clear();
    return buffer.array();
}

In the hashmap for all the bitmaps I have I put: 在我放入的所有位图的哈希图中:

    pixels.put(bitmap1, getPixels(b)); 

And when I want to get the value (bytes) back I do: 当我想取回值(字节)时,我会做:

    byte[] pixelData = pixels.get(bitmap1);

and for some odd reason its always null!, why? 出于某种奇怪的原因,它始终为空!,为什么? I tried on different bitmaps, they all return null, and it is the same bitmap.. 我尝试了不同的位图,它们都返回null,并且它是同一位图。

It must be the same instance of Bitmap. 它必须是位图的相同实例。 If you create another instance of same Bitmap, it'll not be the same object. 如果创建相同位图的另一个实例,则它将不是同一对象。

For instance: 例如:

Bitmap b1 = BitmapFactory.decodeFile("mybitmap.png");
ConcurrentHashMap<Bitmap, byte[]> pixels = new ConcurrentHashMap<Bitmap, byte[]>();
pixels.add(b1);
[...]
Bitmap b2 = BitmapFactory.decodeFile("mybitmap.png");
byte[] barray1 = pixels.get(b1);
byte[] barray2 = pixels.get(b2);

barray1 is not null barray1不为null

barray2 is null barray2为空

First things: the javadoc of Android's Bitmap makes no guarantee that it supports equals() and hashCode() if they have the same content, which means that if they don't, you're screwed. 第一件事:Android的Bitmap的javadoc无法保证如果它们具有相同的内容,则支持equals()hashCode() ,这意味着如果它们不相同,那么您就搞砸了。

Second: the javadoc of ByteBuffer.allocateDirect() states that: 第二: ByteBuffer.allocateDirect()的javadoc指出:

Whether or not it has a backing array is unspecified. 不确定是否具有支持数组。

Therefore --> don't use that, use ByteBuffer.allocate() instead which is guaranteed to have a backing array. 因此->不要使用它,而是使用ByteBuffer.allocate() 保证具有支持数组。

But I guess the problem is with the first point. 但是我想问题出在第一点。

Whenever you use a HashMap and you treat a certain type of object as a key , you need to consider whether that object's equals() method is designed properly for custom equality checks. 每当使用HashMap并将某种类型的对象作为键时 ,都需要考虑该对象的equals()方法是否为自定义相等性检查设计了适当的对象。 In most cases it is not, so you need to re-implement the equals() method. 在大多数情况下不是这样,因此您需要重新实现equals()方法。

The prevailing logic in Java is that when you override equals() , you must also override the hashCode() method. Java中流行的逻辑是,当您覆盖equals() ,还必须覆盖hashCode()方法。 (See this post ) (请参阅这篇文章

In other words, you need to derive a class CustomBitmap from Bitmap and add new implementations of equals() and hashCode() to the new class. 换句话说,您需要从Bitmap派生一个CustomBitmap类,并将equals()hashCode()新实现添加到新类中。 Then it can be used successfully as a key in a HashMap . 然后,它可以成功用作HashMap的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM