Java编程思想: 容器深入研究
程序员文章站
2024-03-17 17:41:22
...
完整的容器分类法
填充容器
进行容器的填充时, 我们可以使用Collections.nCopies()来创建一个List, 或者使用fill来进行填充.
import java.util.*;
class StringAddress {
private String s;
public StringAddress(String s) {this.s = s;}
public String toString() {
return super.toString() + " " + s;
}
}
public class FillingLists {
public static void main(String[] args) {
List<StringAddress> list = new ArrayList<>(Collections.nCopies(4, new StringAddress("Hello")));
// [aaa@qq.com Hello, aaa@qq.com Hello, aaa@qq.com Hello, aaa@qq.com Hello]
System.out.println(list);
Collections.fill(list, new StringAddress("World"));
// [aaa@qq.com World, aaa@qq.com World, aaa@qq.com World, aaa@qq.com World]
System.out.println(list);
}
}
一种Generator解决方案
我们可以定义一个生成器来进行容器的操作:
import java.util.*;
interface Generator<T> {
T next();
}
class CollectionData<T> extends ArrayList<T> {
public CollectionData(Generator<T> gen, int quantity) {
for (int i = 0; i < quantity; i++) {
add(gen.next());
}
}
public static <T> CollectionData<T> list(Generator<T> gen, int quantity) {
return new CollectionData<>(gen, quantity);
}
}
class Government implements Generator<String> {
String[] foundation = ("strange women lying in ponds distributing swords is no basis for a system of government").split(" ");
private int index;
public String next() { return foundation[index++]; }
}
public class CollectionDataTest {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>(new CollectionData<>(new Government(), 15));
System.out.println(set);
set.addAll(CollectionData.list(new Government(), 15));
System.out.println(set);
}
}
Map生成器
import java.util.LinkedHashMap;
class Pair<K, V> {
public final K key;
public final V value;
public Pair(K k, V v) {
key = k;
value = v;
}
}
public class MapData<K, V> extends LinkedHashMap<K, V> {
public MapData(Generator<Pair<K, V>> gen, int quantity) {
for (int i = 0; i < quantity; i++) {
Pair<K, V> p = gen.next();
put(p.key, p.value);
}
}
public MapData(Generator<K> genK, Generator<V> genV, int quantity) {
for (int i = 0; i < quantity; i++) {
put(genK.next(), genV.next());
}
}
public MapData(Generator<K> genK, V value, int quantity) {
for (int i = 0; i < quantity; i++) {
put(genK.next(), value);
}
}
public MapData(Iterable<K> genK, Generator<V> genV) {
for (K key: genK) {
put(key, genV.next());
}
}
public MapData(Iterable<K> genK, V value) {
for (K key: genK) {
put(key, value);
}
}
public static <K, V> MapData<K, V> map(Generator<Pair<K, V>> gen, int quantity) {
return new MapData<K, V>(gen, quantity);
}
public static <K, V> MapData<K, V> map(Generator<K> genK, Generator<V> genV, int quantity) {
return new MapData<K, V>(genK, genV, quantity);
}
public static <K, V> MapData<K, V> map(Generator<K> genK, V value, int quantity) {
return new MapData<K, V>(genK, value, quantity);
}
public static <K, V> MapData<K, V> map(Iterable<K> genK, Generator<V> genV) {
return new MapData<K, V>(genK, genV);
}
public static <K, V> MapData<K, V> map(Iterable<K> genK, V value) {
return new MapData<K, V>(genK, value);
}
}
我们可以使用封装好的MapData类:
import java.util.*;
class Letters implements Generator<Pair<Integer, String>>, Iterable<Integer> {
private int size = 9;
private int number = 1;
private char letter = 'A';
public Pair<Integer, String> next() {
return new Pair<Integer, String>(number++, "" + letter);
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
public Integer next() { return number++; }
public boolean hasNext() { return number < size; }
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
public class MapDataTest {
public static void main(String[] args) {
// {1=A, 2=A, 3=A, 4=A, 5=A, 6=A, 7=A, 8=A, 9=A, 10=A, 11=A}
System.out.println(MapData.map(new Letters(), 11));
}
}
使用Abstract类
我们可以通过继承其Abstract类来定制Map,Set等.
转载于:https://my.oschina.net/voler/blog/724266