Map生成器 map适配器现在可以使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象
package org.rui.collection2.map; /** * map生成器 * @author lenovo * */ public class Pair<K,V> { public final K key; public final V value; public Pair(K k,V v) { this.key=k; this.value=v; } }
//Generator.java package org.rui.generics.anonymity; public interface Generator<T> { //返回泛型的内型对象 T next(); }
package org.rui.collection2.map; import java.util.Iterator; import java.util.LinkedHashMap; import org.rui.generics.anonymity.Generator; /** * map 适配器现在可以使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象 * @author lenovo * * @param <K> * @param <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()); } } //////////A key Generator and a single value///////////////////////////////////////////////// public MapData(Generator<K> genK,V genV,int quantity) { for(int i=0;i<quantity;i++) { put(genK.next(),genV); } } ///////////an iterable and a value generator//////////////////////////////////////////////// public MapData(Iterable<K> genK,Generator<V> genV) { for(K k : genK) { put(k,genV.next()); } } ///////////an iterable and a single value//////////////////////////////////////////////// public MapData(Iterable<K> genK,V v) { for(K k : genK) { //System.out.println(k); put(k,v); } } /////////////generic convenience methods/////////////////////////////////////////// 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> gen,Generator<V> genV,int quantity) { return new MapData<K,V>(gen,genV,quantity); } public static<K,V> MapData<K,V> map(Generator<K> gen,V v,int quantity) { return new MapData<K,V>(gen,v,quantity); } public static<K,V> MapData<K,V> map(Iterable<K> k,Generator<V> v) { return new MapData<K,V>(k,v); } public static<K,V> MapData<K,V> map(Iterable<K> k,V v) { return new MapData<K,V>(k,v); } }
package org.rui.collection2.map; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.rui.generics.anonymity.Generator; /** * 下面是一个使用MapData的示例,LettersGenerator * 通过产生一个Iterator还实现了Iterable,通过这种方式, * 它可以被用来测试MapData.map()方法,而这些方法都需要用到iterable * @author lenovo * */ class Letters implements Generator<Pair<Integer,String>>,Iterable<Integer> { private int size=9; private int number=1; private char letter='a'; @Override public Pair<Integer, String> next() { return new Pair<Integer,String>(number++,""+letter++); } @Override 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) { Character[] chars={'a','b','c','d','e'}; List<Character> list = Arrays.asList(chars); System.out.println(MapData.map(new Letters(),11)); System.out.println(MapData.map(new Letters(),"pop")); System.out.println(MapData.map(new Letters(),new Letters())); System.out.println(MapData.map(list,"value")); } } /**output: {1=a, 2=b, 3=c, 4=d, 5=e, 6=f, 7=g, 8=h, 9=i, 10=j, 11=k} {1=pop, 2=pop, 3=pop, 4=pop, 5=pop, 6=pop, 7=pop, 8=pop} {[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]} {a=value, b=value, c=value, d=value, e=value} */
Map生成器 map适配器现在可以使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象
时间: 2024-11-01 17:24:56