Android HashMap 序列化/反序列化

发布于 2024-11-07 14:13:11 字数 210 浏览 0 评论 0原文

您好,我有一个位图 Hasmap,我需要将其存储在 Android 设备上,以便在下次应用程序启动时使用。

我的哈希图如下所示,最多包含 1000 个位图:

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

Hello I have a Hasmap of bitmaps which I need to store on the Android device to be used when next the application starts.

My hashmap looks like this, and contains up to 1000 Bitmaps:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-11-14 14:13:11

您可能需要考虑创建 Map 扩展(通过使用 AbstractMap)并覆盖相关函数。一般来说,扩展的结构应该具有:

  1. 使用常规映射的内存硬缓存。这应该是一个大小限制的缓存对象。您可以利用 LinkedHashMap 并重写removeEldesEntry() 来检查大小是否超出

    this.objectMap = Collections.synchronizedMap(new LinkedHashMap()  {
        @Override
        protected boolean removeEldestEntry(LinkedHashMap.Entry  eldest) {
            if (size() > HARD_CACHE_CAPACITY) {
                // remove from cache, pass to secondary SoftReference cache or directly to the disk
            }
        }
    });
  1. 如果超出缓存,则将其放入磁盘
  2. 重写 get 函数以执行以下操作: 在初始获取时,根据特定命名约定从磁盘加载位图(与密钥相关)并将其存储在内存中。大致类似于(请原谅任何语法错误)


    @Override
    public Bitmap get(Object key) {
        if(key != null) {
            // first level, hard cache
            if(objectMap.containsKey(key)) {
                return objectMap.get(key);
            }

            // soft reference cache
            if(secondaryCache.containsKey(key)) {
                return secondaryCache.get(key);
            }

            // get from disk if it is not in hard or soft cache
            String fileName = "Disk-" + key + ".txt";
            File f = new File(cacheDir, fileName);

            if(f.exists()) {
                // put this back to the hard cache
                Bitmap object = readFromReader(f);

                if(object != null) {
                    objectMap.put((String)key, object);
                    return object;
                }
            }
        }


        return null;  // unable to get from any data source
    }

类似地,您的放置必须被覆盖才能放入磁盘以供以后使用,因此当您重新初始化应用程序时,您可以只创建地图扩展的实例。如果需要,您还可以按应用程序中最近使用的项目预加载哈希图。基本上,通过扩展 AbstractMap,您可以获得灵活性,而不会因这 1000 个位图而占用您的内存。希望这有帮助

You might want to consider create extension of Map (by using AbstractMap) and override the related functions. In general the structure of the extension should have:

  1. An in memory hard cache using regular Map. This should be a size bound cache object. You could leverage LinkedHashMap and override removeEldesEntry() to check if the size is exceeded

    this.objectMap = Collections.synchronizedMap(new LinkedHashMap()  {
        @Override
        protected boolean removeEldestEntry(LinkedHashMap.Entry  eldest) {
            if (size() > HARD_CACHE_CAPACITY) {
                // remove from cache, pass to secondary SoftReference cache or directly to the disk
            }
        }
    });
  1. If the cache is exceeded, then put it to disk
  2. Override the get function to do the following : On initial get, load the bitmap from disk based on certain naming convention (related to the key) and store it in memory. Roughly something like (please forgive any syntax error)


    @Override
    public Bitmap get(Object key) {
        if(key != null) {
            // first level, hard cache
            if(objectMap.containsKey(key)) {
                return objectMap.get(key);
            }

            // soft reference cache
            if(secondaryCache.containsKey(key)) {
                return secondaryCache.get(key);
            }

            // get from disk if it is not in hard or soft cache
            String fileName = "Disk-" + key + ".txt";
            File f = new File(cacheDir, fileName);

            if(f.exists()) {
                // put this back to the hard cache
                Bitmap object = readFromReader(f);

                if(object != null) {
                    objectMap.put((String)key, object);
                    return object;
                }
            }
        }


        return null;  // unable to get from any data source
    }

Similarly your put has to be override to put to the disk for later use, so when you reinitialize your app you could just create an instance of the map extension. If you want, you could also preload the hashmap by most recently used items in the app. Basically, by extending the AbstractMap, you get the flexibilities without killing your memory with that 1000 bitmaps. Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文