-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapData.java
More file actions
51 lines (41 loc) · 1.25 KB
/
MapData.java
File metadata and controls
51 lines (41 loc) · 1.25 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Collection.Generator;
import java.util.LinkedHashMap;
import java.util.Map;
/*
* 产生Map的适配器类
*
* */
public class MapData<K,V> extends LinkedHashMap<K, V>{
//一个单独的Generator,里面存放Pair类
public MapData(Generator<Pair<K, V>> generator,int quantity){
for(int i=0;i<quantity;i++){
Pair<K, V> pair= generator.next();
put(pair.key, pair.value);
}
}
//两个分离的Generator,分别代表key和value
public MapData(Generator<K> keys,Generator<V> values,int quantity){
for(int i=0;i<quantity;i++){
K key=keys.next();
V value=values.next();
put(key, value);
}
}
//一个Generator,一个单独的value
public MapData(Generator<K> keys,V value,int quantity){
for(int i=0;i<quantity;i++){
K key=keys.next();
put(key, value);
}
}
//一些简便的实例化MapData方法
public static <K,V> MapData<K, V> map(Generator<Pair<K, V>> generator,int quantity){
return new MapData<>(generator, quantity);
}
public static <K,V>MapData<K, V> map(Generator<K> keys,Generator<V> values,int quantity){
return new MapData<>(keys,values, quantity);
}
public static <K,V>MapData<K, V> Map(Generator<K> keys,V value,int quantity){
return new MapData<K, V>(keys, value, quantity);
}
}