Java高性能集合类 ConcurrentLinkedHashMap demo
2015-09-11 17:50
225 查看
ConcurrentLinkedHashMap是java.util.LinkedHashMap的一个高性能实现。主要用于软件缓存。
ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对
ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见
http://code.google.com/p/concurrentlinkedhashmap
https://github.com/ben-manes/concurrentlinkedhashmap
ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对
ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见
http://code.google.com/p/concurrentlinkedhashmap
https://github.com/ben-manes/concurrentlinkedhashmap
import java.util.Map; import java.util.concurrent.ConcurrentMap; import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import com.googlecode.concurrentlinkedhashmap.EvictionListener; import com.googlecode.concurrentlinkedhashmap.Weighers; public class Test { public static void main(String[] args) { //test001(); test002(); } private static void test001() { EvictionListener<String, String> listener = new EvictionListener<String, String>() { @Override public void onEviction(String key, String value) { System.out.println("Evicted key=" + key + ", value=" + value); } }; ConcurrentMap<String, String> cache = new ConcurrentLinkedHashMap.Builder<String, String>() .maximumWeightedCapacity(10).listener(listener).build(); for (int i = 0; i < 150; i++) { int j = 1024; j = j + i; cache.put(String.valueOf(j), "nihao" + i); } for (Map.Entry<String, String> entry : cache.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + "====" + value); } System.out.println(cache.get("1025")); cache.remove("1026"); } /** ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对 ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见 http://code.google.com/p/concurrentlinkedhashmap */ private static void test002() { ConcurrentLinkedHashMap<Integer, Integer> map = new ConcurrentLinkedHashMap.Builder<Integer, Integer>() .maximumWeightedCapacity(2).weigher(Weighers.singleton()) .build(); map.put(1, 1); map.put(2, 2); map.put(3, 3); System.out.println(map.get(1));// null 已经失效了 System.out.println(map.get(2)); } }
相关文章推荐
- Java工厂模式
- 从mysql数据库生成javabean
- Android Studio教程04-导入Eclipse项目到AndroidStudio(包括导入第三方Project项目(不是jar包形式))
- java保留两位小数
- Java+jsp实现简单登录
- java学习经典书籍_杂篇
- Java代码生成器--告别无止境的增删改查
- Java代码生成器--告别无止境的增删改查
- Java代码生成器--告别无止境的增删改查
- Java代码生成器--告别无止境的增删改查
- Java代码生成器--告别无止境的增删改查
- java__线程通信
- java学习经典书籍_数据库篇
- java之int与integer的区别
- Android:eclipse启动时出现异常Failed to initialize Monitor Thread: Unable to establish loopback connection
- java学习经典书籍_移动开发篇
- Ubuntu 14.04 安装 JDK 8,ubuntu14.04
- springMVC文件下载
- java实现4个线程对一个公共变量修改
- 经典书籍_java学习基础编程篇