google collection工具包的MapMaker使用

简介:

摘自网上描述语段:

Google Collections中的MapMaker融合了Weak Reference线程安全高并发性能异步超时清理自定义构建元素等强大功能于一身。

常阅读优秀源代码的童鞋都知道,一般叫Maker的对象都是Builder模式,而这个MapMaker就是来”Build“Map的.

一、google collection工具包的MapMaker使用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void main(String[] args) {
         /**
          * expiration(3, TimeUnit.SECONDS)设置超时时间为3秒
          */
         ConcurrentMap<String , String> map = new MapMaker().concurrencyLevel(32).softKeys().weakValues()
                 .expiration(3, TimeUnit.SECONDS).makeComputingMap(
                         /**
                          * 提供当Map里面不包含所get的项,可以自动加入到Map的功能
                          * 可以将这里的返回值放到对应的key的value中
                          */
                         new Function<String, String>() {
                             public String apply(String s) {
                                 return "creating " + s + " -> Object" ;
                             }
                         }
                 );
 
         map.put( "a" , "testa" );
         map.put( "b" , "testb" );
 
         System. out .println(map. get ( "a" ));
         System. out .println(map. get ( "b" ));
         System. out .println(map. get ( "c" ));
 
         try {
             // 4秒后,大于超时时间,缓存失效。
             Thread.sleep(4000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
 
         System. out .println(map. get ( "a" ));
         System. out .println(map. get ( "b" ));
         System. out .println(map. get ( "c" ));
     }

结果如下:

?
1
2
3
4
5
6
testa
testb
creating c -> Object
creating a -> Object
creating b -> Object
creating c -> Object

 

二、先看下其api的相关demo片段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 使用案例:存储验证码
     // <String, String> == <用户唯一,验证码>
     // expiration(15, TimeUnit.MINUTES) 有效期15分钟
     ConcurrentMap<String,String> capthcaMap = new MapMaker().expiration(15, TimeUnit.MINUTES).makeMap();
 
     // 设置ConcurrentMap的concurrencyLevel参数 ,例如ConcurrentHashMap是用来控制其Segment数组的大小
     ConcurrentMap<String,Object> map1 = new MapMaker().concurrencyLevel(8).makeMap();
 
     // 构造各种不同reference作为key和value的map
     ConcurrentMap<String,Object> map2 = new MapMaker().softKeys().weakValues().makeMap();
 
     // 提供当Map里面不包含所get的项,可以自动加入到Map的功能
     ConcurrentMap<String,Integer> map3 = new MapMaker()
             .makeComputingMap(
                     new Function<String, Integer>() {
                         public Integer apply(String key) {
                             return 1;
                         }
                     }
             );

可以看出过了4秒后,缓存失效,所以呈现如此结果。


相关文章
|
存储 移动开发
【Sets】使用Google Guava工程中Sets工具包,实现集合的并集/交集/补集/差集
获取两个txt文档的内容~存储进集合中求集合的并集/交集/补集/差集 1 package com.sxd.readLines.aboutDB; 2 3 import java.io.BufferedReader; 4 import java.
2730 0
|
4月前
|
ice
Google Earth Engine ——Landsat 8 影像集合Collection详细介绍
Google Earth Engine ——Landsat 8 影像集合Collection详细介绍
92 2
|
Java API Maven
Guava:google公司开发的一款Java类库扩展工具包
Guava:google公司开发的一款Java类库扩展工具包
364 0
|
传感器 数据采集 ice
Google Earth Engine ——Landsat 4 TM Collection 1 Tier 1 DN值经过缩放、校准的传感器辐射度——8天/32天/年际合成数据集
Google Earth Engine ——Landsat 4 TM Collection 1 Tier 1 DN值经过缩放、校准的传感器辐射度——8天/32天/年际合成数据集
138 0
Google Earth Engine ——Landsat 4 TM Collection 1 Tier 1 DN值经过缩放、校准的传感器辐射度——8天/32天/年际合成数据集
|
传感器 数据采集
Google Earth Engine ——LANDSAT 7/LM01/C01/T1Landsat 1 MSS Collection 1 Tier 1 DN值,代表经过缩放、校准的传感器辐射度影像数
Google Earth Engine ——LANDSAT 7/LM01/C01/T1Landsat 1 MSS Collection 1 Tier 1 DN值,代表经过缩放、校准的传感器辐射度影像数
147 0
Google Earth Engine ——LANDSAT 7/LM01/C01/T1Landsat 1 MSS Collection 1 Tier 1 DN值,代表经过缩放、校准的传感器辐射度影像数
|
传感器 数据采集 ice
Google Earth Engine ——LANDSAT 7Collection 1 Tier 1 DN值 RAW数据集
Google Earth Engine ——LANDSAT 7Collection 1 Tier 1 DN值 RAW数据集
196 0
Google Earth Engine ——LANDSAT 7Collection 1 Tier 1 DN值 RAW数据集
|
传感器 数据采集 ice
Google Earth Engine ——LANDSAT 7 Collection 1 Tier 1 and Real-Time data DN values数据集
Google Earth Engine ——LANDSAT 7 Collection 1 Tier 1 and Real-Time data DN values数据集
128 0
Google Earth Engine ——LANDSAT 7 Collection 1 Tier 1 and Real-Time data DN values数据集
|
传感器 数据采集 关系型数据库
Google Earth Engine ——Landsat 8 Collection 1 Tier 1数据集
Google Earth Engine ——Landsat 8 Collection 1 Tier 1数据集
165 0
Google Earth Engine ——Landsat 8 Collection 1 Tier 1数据集
|
存储 Web App开发 安全
Google发布Native Client首个开发工具包
Google的产品经理Christian Stefansen 今天在Chromium官方博客宣布Native Client首个软件开发工具包(SDK)发布,开发者已经可以通过这里下载到 Native Client 的开发工具包,自己创建Native Client应用程序。
774 0

热门文章

最新文章

下一篇
云函数