ConcurrentLinkedHashMap
最近发现了一个好东西,基于LRU策略的缓存,说白了就是热点数据缓存;public class ConcurrentLinkedHashMapTest {
public class ConcurrentLinkedHashmapTest {
public static void main(String[] args) {
test01();
}
public static void test01() {
ConcurrentLinkedHashMap<Long, String> map = new ConcurrentLinkedHashMap.Builder<Long, String>()
.maximumWeightedCapacity(2).weigher(Weighers.singleton()).build();
map.put(01L, "hello");
map.put(02L, "zl");
System.out.println(map.get(01L));
map.put(03L, "test");
System.out.println(map.get(02L));
System.out.println("over");
}
}
输出:hello null over
设置集合大小,以及数据权重,超过集合大小会去掉最早添加且没用到的数据;
public class ConcurrentLinkedHashmapTest { public static void main(String[] args) { test01(); } public static void test01() { ConcurrentLinkedHashMap<Long, String> map = new ConcurrentLinkedHashMap.Builder<Long, String>() .maximumWeightedCapacity(2).weigher(Weighers.singleton()).build(); map.put(01L, "hello"); map.put(02L, "zl"); map.put(03L, "test"); System.out.println(map.get(01L)); System.out.println("over"); } }
输出:null over
我们曾如此渴望生命的波澜,到后来才发现,人生最曼妙的风景是内心的淡定与从容