您的位置:首页 > 编程语言 > Java开发

从头认识java-15.1 填充容器(3)-填充Map

2015-12-27 16:32 453 查看
这一章节我们来讨论一下填充容器的另一个方面Map,之前的两个章节我们都是用list来作为容器,这一章节我们使用Map。
还有在这里解释一下为什么一直都使用生成器这个东西,其实他就是建造者设计模式,它主要的作用就是生产复杂的对象,而且满足各种需求的变化(灵活性)。
还有为什么花这么多章节来讨论填充容器,主要因为填充容器包括比较多的知识点,知识点列举:
(1)泛型
(2)建造者设计模式
(3)容器的填充方法(list 的add,map的put等)
进入主题,我们来讨论一下Map的填充
1.例子

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().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);
		}
	}
}

输出:
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的填充。

这一章节就到这里,谢谢。-----------------------------------目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: