本文實例講述了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程序設計有所幫助。
|
新聞熱點
疑難解答