本文实例讲述了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缓存保存数据的方法。