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

posted @ 2019-10-24 18:31  zlAdmin  阅读(2070)  评论(1)    收藏  举报