首页 > 编程 > Java > 正文

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

2019-11-26 13:59:05
字体:
来源:转载
供稿:网友

本文实例讲述了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程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表