java使用hashMap缓存保存数据的方法


本文实例讲述了java使用hashMap缓存保存数据的方法。分享给大家供大家参考,具体如下:

private static final HashMap<Long, XXX> sCache = new HashMap<Long, XXX>();
private static int sId = -1;
public static void initAlbumArtCache() {
  try {
    //。。。
    if (id != sId) {
      clearCache();
      sId = id; 
    }
  } catch (RemoteException e) {
    e.printStackTrace();
  }
}
public static void clearCache() {
  synchronized(sCache) {
    sCache.clear();
  }
}
public static XXX getCachedXXX(long Index, BitmapDrawable defaultBitmap) {
  XXX d = null;
  synchronized(sCache) {
    d = sCache.get(Index);
  }
  if (d == null) {
    //。。。
    synchronized(sArtCache) {
      // the cache may have changed since we checked
      XXX value = sCache.get(Index);
      if (value == null) {
        sCache.put(Index, d);
      } else {
        d = value;
      }
    }
  }
  return d;
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3