Serialize / Deserialize the HashMap TV Series

Hello, I have a Hasmap from bitmaps that I need to store on an Android device that will be used the next time the application starts.

My hash file looks like this and contains up to 1000 bitmaps:

private static HashMap <String, Bitmap> cache = new HashMap<String, Bitmap>();
+3
source share
1 answer

You might want to consider creating a Map extension (using AbstractMap) and overriding related functions. In general, the extension structure should have:

  • The hard memory cache uses a regular card. This must be a size cache object. You can use 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
            }
        }
    });
  • If the cache is exceeded, then put it on the disk
  • get, : 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
    }

, put , . , - . , AbstractMap, , 1000 . ,

0

All Articles