就像数组一样,Arrays.fill是填充方法,在容器里面也有。
1.Collections.nCopies
这个方法是生成某种类型多少个对象,然后我们可以把他放到容器的构造函数里面,填充这个容器。
例子:
[java] view plain copy
-
-
package com.ray.ch15; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; public class Test { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(5, 1)); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } LinkedList<Integer> linkedList = new LinkedList<Integer>( Collections.nCopies(6, 2)); for (int i = 0; i < linkedList.size(); i++) { System.out.print(linkedList.get(i) + " "); } } }
输出:
1 1 1 1 1 2 2 2 2 2 2
2.fill
这里的fill不要被他的名字迷惑,它的作用是替换容器里面的对象。
[java] view plain copy
-
-
package com.ray.ch15; import java.util.ArrayList; import java.util.Collections; public class Test { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(5, 1)); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); Collections.fill(list, 2); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } }
输出:
1 1 1 1 1 2 2 2 2 2
另一种填充容器的方式:addAll
例子:
-
[java] view plain copy package com.ray.ch15; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; public class Test { public static void main(String[] args) { MyCollection<String> myCollection = new MyCollection<String>( new MyGenerator(), 25);//在构造器填充 System.out.println(Arrays.toString(myCollection.toArray())); LinkedHashSet<String> set = new LinkedHashSet<String>(myCollection); System.out.println(Arrays.toString(set.toArray())); set.clear(); set.addAll(myCollection);//另一种方式 System.out.println(Arrays.toString(set.toArray())); } } interface Generator<T> { T next(); } class MyCollection<T> extends ArrayList<T> { private static final long serialVersionUID = 1L; public MyCollection(Generator<T> generator, int count) { for (int i = 0; i < count; i++) { add(generator.next()); } } } class MyGenerator implements Generator<String> { private String strPool = "The annual expansion rate for " + "industrial output will be around 6 percent this year, " + "well below this year's GDP growth, which is likely to be " + "about 7 percent, the Ministry of Industry and Information " + "Technology said, adding that such a situation was happening " + "for the first time in nearly two decades."; private int index = 0; @Override public String next() { return strPool.split(" ")[index++]; } }
输出:
[The, annual, expansion, rate, for, industrial, output, will, be, around]
[The, annual, expansion, rate, for, industrial, output, will, be, around]
[The, annual, expansion, rate, for, industrial, output, will, be, around]
我来解释一下上面的代码,有几个需要注意的地方:
(1)上面继续前几个篇幅所提到的生成器,我们通过泛型生成器来生成相关的对象。这种方式创建的对象具备灵活性,不像系统提供的nCopy方法,只能创建单一的对象。
(2)创建一个自己的容器(继承Arraylist),它是用来装载生成器创建的对象,继承某个容器,可以方便的在构造器里面调用add方法,也具备容器的特性
(3)在main方法里面展现了两种填充容器的方式,一个是直接在构造器里面添加,一个是使用addAll方法,这两种方法都是接收Collection类型的容器,因此我们无论生成什么的容器,都可以add进去。
下填充容器的另一个方面Map,之前的两个章节我们都是用list来作为容器,这一章节我们使用Map。
还有在这里解释一下为什么一直都使用生成器这个东西,其实他就是建造者设计模式,它主要的作用就是生产复杂的对象,而且满足各种需求的变化(灵活性)。
还有为什么花这么多章节来讨论填充容器,主要因为填充容器包括比较多的知识点,知识点列举:
(1)泛型
(2)建造者设计模式
(3)容器的填充方法(list 的add,map的put等)
进入主题,我们来讨论一下Map的填充
1.例子
-
[java] view plain copy package com.ray.ch14; import java.util.HashMap; import java.util.Random; public class Test { public static void main(String[] args) { MyMap<Integer, String> myMap = new MyMap<Integer, String>( new LetterGenerator(), 10); for (Integer key : myMap.keySet()) { System.out.println("key:" + key + " value:" + myMap.get(key)); } new HashMap<integer string="">().putAll(myMap);// 这样就可以通过putAll生成一组对象。 } } interface Generator<T> { T next(); } class LetterGenerator implements Generator<Pair<Integer, String>> { private String str = "The PLA Daily must adhere to the leadership " + "of the Communist Party of China (CPC) and serve the PLA, " + "which is also under the CPC leadership, said Xi, who is " + "also general secretary of the CPC Central Committee and " + "chairman of the Central Military Commission (CMC)."; private Integer index = str.split(" ").length - 1; @Override public Pair<Integer, String> next() { int param = new Random().nextInt(index); return new Pair<Integer, String>(param, str.split(" ")[param]); } } class Pair<K, V> { public final K key; public final V value; public Pair(K key, V value) { this.key = key; this.value = value; } } @SuppressWarnings("serial") class MyMap<K, V> extends HashMap<K, V> { public MyMap(Generator<Pair<K, V>> generator, int count) { for (int i = 0; i < count; i++) { put(generator.next().key, generator.next().value); } } } </integer>
输出:
key:1 value:adhere
key:32 value:chairman
key:2 value:the
key:21 value:CPC
key:23 value:PLA
key:22 value:to
key:25 value:leadership,
key:24 value:CPC
key:9 value:China
key:30 value:serve
解释一下上面的代码:
(1)目的:生成一组(数字,字符串)的Map,数字和字符串都是随机的
(2)我们需要组装类Pair,因为需要填充Map,Pair 的Key和Value我们都是标注为final,这样方面使用。
(3)LetterGenerator实现Generator,然后把所需要的对象组装成Pair
(4)MyMap继承HashMap,扩展新的构造器
(5)通过Map里面的putAll或者Collections.addAll方法,就可以生产一个新的Map
2.我们修改一下上面的例子,变换MyMap构造器(这里的构造器可以放在一起,但是放在一起代码会比较长,因此我们变换了构造器,而不是在上面增加),以满足各种的需求。
package com.ray.ch14; import java.util.HashMap; import java.util.Random; public class Test { public static void main(String[] args) { MyMap<Integer, String> myMap = new MyMap<Integer, String>( new KeyGenerator(), new ValueGenerator(), 10); for (Integer key : myMap.keySet()) { System.out.println("key:" + key + " value:" + myMap.get(key)); } new HashMap<Integer, String>().putAll(myMap);// 这样就可以通过putAll生成一组对象。 } } interface Generator<T> { T next(); } class KeyGenerator implements Generator<Integer> { private Integer index = 10; @Override public Integer next() { return new Random().nextInt(index); } } class ValueGenerator implements Generator<String> { private String str = "The PLA Daily must adhere to the leadership " + "of the Communist Party of China (CPC) and serve the PLA, " + "which is also under the CPC leadership, said Xi, who is " + "also general secretary of the CPC Central Committee and " + "chairman of the Central Military Commission (CMC)."; @Override public String next() { return str.split(" ")[new Random().nextInt(str.split(" ").length - 1)]; } } @SuppressWarnings("serial") class MyMap<K, V> extends HashMap<K, V> { public MyMap(Generator<K> keyGenerator, Generator<V> valueGenerator, int count) { for (int i = 0; i < count; i++) { put(keyGenerator.next(), valueGenerator.next()); } } }
输出:
key:0 value:to
key:1 value:CPC
key:3 value:Central
key:6 value:the
key:7 value:the
key:8 value:and
key:9 value:under
上面的代码我们把Pair这个组合类分开来实现。
总结:我们上面介绍了Map的填充。