Java集合(2):两个生成器的例子:Collection生成器CollectionData及Map生成器MapData
Collection生成器CollectionData
CollectionData体现了适配器模式的设计思想,它能把实现Generator接口的类的对象(包括上一章数组中的各种RandomGenerator)都复制到自身当中。
1 import java.util.ArrayList; 2 import java.util.LinkedHashSet; 3 import java.util.Set; 4 5 interface Generator<T> { 6 T next(); 7 } 8 9 class CollectionData<T> extends ArrayList<T> { 10 private static final long serialVersionUID = 1L; 11 public CollectionData(Generator<T> gen, int quantity) { 12 for (int i = 0; i < quantity; i++) 13 add(gen.next()); 14 } 15 public static <T> CollectionData<T> list(Generator<T> gen, int quantity) { 16 return new CollectionData<T>(gen, quantity); 17 } 18 } 19 20 class Animal implements Generator<String> { 21 private String[] items = { "Dog", "Pig", "Cat" }; 22 private int index; 23 @Override 24 public String next() { 25 return items[index++]; 26 } 27 } 28 29 public class Test2 { 30 public static void main(String[] args) { 31 Set<String> set = new LinkedHashSet<String>(new CollectionData<String>(new Animal(), 3)); 32 System.out.println(set); // [Dog, Pig, Cat] 33 set.addAll(CollectionData.list(new Animal(), 3)); 34 System.out.println(set); // [Dog, Pig, Cat] 35 } 36 }
Map生成器MapData
我们可以使用同样的方式生成Map,我们可以Generator<K>,Generator<V>分开生成,也可以使用Pair<K, V>组装好K,V再生成。
1 import java.util.Iterator; 2 3 import java.util.LinkedHashMap; 4 5 class Pair<K, V> { 6 public final K key; 7 public final V value; 8 9 public Pair(K k, V v) { 10 this.key = k; 11 this.value = v; 12 } 13 } 14 15 class MapData<K, V> extends LinkedHashMap<K, V> { 16 private static final long serialVersionUID = 1L; 17 18 // A single Pair Generator: 19 public MapData(Generator<Pair<K, V>> gen, int quantity) { 20 for (int i = 0; i < quantity; i++) { 21 Pair<K, V> p = gen.next(); 22 put(p.key, p.value); 23 } 24 } 25 26 public static <K, V> MapData<K, V> map(Generator<Pair<K, V>> gen, int quantity) { 27 return new MapData<K, V>(gen, quantity); 28 } 29 30 // Two separate Generators: 31 public MapData(Generator<K> genK, Generator<V> genV, int quantity) { 32 for (int i = 0; i < quantity; i++) { 33 put(genK.next(), genV.next()); 34 } 35 } 36 37 public static <K, V> MapData<K, V> map(Generator<K> genK, Generator<V> genV, int quantity) { 38 return new MapData<K, V>(genK, genV, quantity); 39 } 40 41 // A key Generator and a single value: 42 public MapData(Generator<K> genK, V value, int quantity) { 43 for (int i = 0; i < quantity; i++) { 44 put(genK.next(), value); 45 } 46 } 47 48 public static <K, V> MapData<K, V> map(Generator<K> genK, V value, int quantity) { 49 return new MapData<K, V>(genK, value, quantity); 50 } 51 52 // An Iterable and a single value: 53 public MapData(Iterable<K> genK, V value) { 54 for (K key : genK) { 55 put(key, value); 56 } 57 } 58 59 public static <K, V> MapData<K, V> map(Iterable<K> genK, V value) { 60 return new MapData<K, V>(genK, value); 61 } 62 } 63 64 class Letters implements Generator<Pair<Integer, String>>, Iterable<Integer> { 65 private int size = 9; 66 private int number = 1; 67 private char letter = 'A'; 68 69 @Override 70 public Pair<Integer, String> next() { 71 return new Pair<Integer, String>(number++, "" + letter++); 72 } 73 74 @Override 75 public Iterator<Integer> iterator() { 76 return new Iterator<Integer>() { 77 @Override 78 public Integer next() { 79 return number++; 80 } 81 82 @Override 83 public boolean hasNext() { 84 return number < size; 85 } 86 87 @Override 88 public void remove() { 89 throw new UnsupportedOperationException(); 90 } 91 }; 92 } 93 } 94 95 public class Test3 { 96 public static void main(String[] args) { 97 // A single Pair Generator test 98 MapData<Integer, String> data1 = MapData.map(new Letters(), 11); 99 System.out.println(data1); // {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K} 100 // An Iterable and a single value test 101 MapData<Integer, String> data2 = MapData.map(new Letters(), "Pop"); 102 System.out.println(data2); // {1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop} 103 } 104 }