File tree 1 file changed +33
-0
lines changed
src/org/javacore/colgoogle
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org .javacore .colgoogle ;
2
+
3
+ import com .google .common .base .Function ;
4
+ import com .google .common .collect .MapMaker ;
5
+
6
+ import java .util .concurrent .ConcurrentMap ;
7
+ import java .util .concurrent .TimeUnit ;
8
+
9
+ /**
10
+ * Created by 子木 on 2016/2/20.
11
+ */
12
+ public class MapMakerT {
13
+ // 使用案例:存储验证码
14
+ // <String, String> == <用户唯一,验证码>
15
+ // expiration(15, TimeUnit.MINUTES) 有效期15分钟
16
+ ConcurrentMap <String ,String > capthcaMap = new MapMaker ().expiration (15 , TimeUnit .MINUTES ).makeMap ();
17
+
18
+ // 设置ConcurrentMap的concurrencyLevel参数 ,例如ConcurrentHashMap是用来控制其Segment数组的大小
19
+ ConcurrentMap <String ,Object > map1 = new MapMaker ().concurrencyLevel (8 ).makeMap ();
20
+
21
+ // 构造各种不同reference作为key和value的map
22
+ ConcurrentMap <String ,Object > map2 = new MapMaker ().softKeys ().weakValues ().makeMap ();
23
+
24
+ // 提供当Map里面不包含所get的项,可以自动加入到Map的功能
25
+ ConcurrentMap <String ,Integer > map3 = new MapMaker ()
26
+ .makeComputingMap (
27
+ new Function <String , Integer >() {
28
+ public Integer apply (String key ) {
29
+ return 1 ;
30
+ }
31
+ }
32
+ );
33
+ }
You can’t perform that action at this time.
0 commit comments