Java hashmap 返回 null

标签 java null hashmap

我从 HashMap 中收到空值。这是 HashMap 的创建:

private HashMap<String,Bitmap> thumbs = new HashMap<String,Bitmap>();

/* adding a single value to the hashmap */

然后我继续从 HashMap 中检索值,如下所示:

    public Bitmap getImageByFileName(String fileName) {

    Bitmap fish = null;
    Iterator it = thumbs.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        fish = (Bitmap)thumbs.get(fileName);        
        it.remove(); 
    }   
    Log.i("shnitzel", " bitmap is " + fish);
    fish = (Bitmap)thumbs.get(fileName);
    Log.i("shnitzel", " final bitmap is " + fish);
    return fish;
}

日志文件:

08-05 22:18:28.170: I/shnitzel(477):  bitmap is android.graphics.Bitmap@40650138
08-05 22:18:28.170: I/shnitzel(477):  final bitmap is null
正如您所看到的,我在“while”循环内部和外部使用完全相同的命令,但由于某种原因它在循环内部起作用,但在外部不起作用。为什么会出现这种情况?

最佳答案

在你的循环中:

it.remove(); 

您在阅读该元素后将其删除。

关于Java hashmap 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18066100/

相关文章:

java - java中Mongodb之间的查询

php - isset 和 !empty 的简写

java - java中简单的空检查时出现NullPointerException

scala - LinkedHashMap 更改为 HashMap 并在 flink 数据流算子中崩溃

java - 为什么我的堆排序比 Java 和 C++ 的排序函数快?

java - SPOJ ADDREV 问题

java - 在打印 JTable 时对齐消息格式

我们可以在 C 中将 (void *)0 称为空指针吗?

java - 根据出现次数打印

c++ - 哈希表键与取值复杂性