首頁  >  文章  >  Java  >  java使用hashMap快取保存資料的方法

java使用hashMap快取保存資料的方法

高洛峰
高洛峰原創
2017-01-19 09:53:402436瀏覽

本文實例講述了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使用hashMap快取保存資料的方法相關文章請關注PHP中文網!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn