您的位置:首页 > 编程语言 > Java开发

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

2016-08-13 10:11 761 查看

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java hashMap 缓存