从头认识java-15.1 填充容器(3)-填充Map
这一章节我们来讨论一下填充容器的还有一个方面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的填充。
这一章节就到这里,谢谢。
-----------------------------------
从头认识java-15.1 填充容器(3)-填充Map的更多相关文章
- 从头认识java-15.1 填充容器(2)-利用Collection的addAll方式
接着上一章节,我们继续介绍填充容器. 这一章节我们结束还有一种填充容器的方式:addAll 样例: package com.ray.ch15; import java.util.ArrayList; ...
- 从头认识java-15.1 填充容器(1)-利用Collection构造器的方式
这一章节我们来介绍一下填充容器. 就像数组一样,Arrays.fill是填充方法,在容器里面也有. 1.Collections.nCopies 这种方法是生成某种类型多少个对象,然后我们能够把他放到容 ...
- 聊聊并发-Java中的Copy-On-Write容器
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp78 聊聊并发-Java中的Copy-On-Write容器 Cop ...
- 由Java 15废弃偏向锁,谈谈Java Synchronized 的锁机制
Java 15 废弃偏向锁 JDK 15已经在2020年9月15日发布,详情见 JDK 15 官方计划.其中有一项更新是废弃偏向锁,官方的详细说明在:JEP 374: Disable and Depr ...
- Java:常用的容器小记
Java:常用的容器小记 对 Java 中的 常用容器,做一个微不足道的小小小小记 容器类概述 常见容器主要包括 Collection 和 Map 两种,Collection 存储着对象的集合,而 M ...
- Java Servlet与Web容器之间的关系
自从计算机软件开发进入网络时代,就开始涉及到通讯问题.在客户/服务器(也叫C/S应用)时期,每个软件都有自己的客户端和服务器端软件.并且客户端和服务器端之间的通讯协议差别也很大.后来随着互联网的发展, ...
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...
- java并发程序——并发容器
概述 java cocurrent包提供了很多并发容器,在提供并发控制的前提下,通过优化,提升性能.本文主要讨论常见的并发容器的实现机制和绝妙之处,但并不会对所有实现细节面面俱到. 为什么JUC需要提 ...
- Java EE中的容器和注入分析,历史与未来
Java EE中的容器和注入分析,历史与未来 java中的容器 java中的注入 容器和注入的历史和展望 一.java中的容器 java EE中的注入,使我们定义的对象能够获取对资源和其他依赖项的引用 ...
随机推荐
- Python装饰器粗解学习
此次学习资料详细来自:http://blog.csdn.net/u013471155 本次是粗学,仍有诸多疑问,暂且记录一二,如有不足和建议,希望可以达者指点. 三个关键点理解: 1.关于函数“变 ...
- Postfix telnet www.azengna.com 25 Connection Refused 但是localhost连接成功
修改配置文件 vi /etc/postfix/main.cf 原先配置信息 .... inet_interfaces = all #inet_interfaces = $myhostname,loca ...
- Backspace doesn't delete inner html tags of a contenteditable DIV in Firefox
https://bugzilla.mozilla.org/show_bug.cgi?id=439808 backspace键 在ff下不能使用 div contenteditable=true时
- selenium grid使用(windows+centos7.4)
windows作为hub,centos7.4作为node. firefox用的centos7自带的52版本,懒得更新. vm虚拟机必须设置成bridge模式上网,否则报错.具体参见博文:Vmware改 ...
- oracle备份表和数据
oracle 备份数据 如果备份表存在 原表t_base_employee,备份表t_base_employee20180718 insert into t_base_employee0718 sel ...
- python模块以及导入出现ImportError: No module named ‘xxx‘问题
python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用如果你要使 ...
- hdu 3732
#include<stdio.h> #include<string.h> int n,m,dp[10001]; int max(int a,int b) { return a ...
- Pollard rho模板
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #in ...
- BZOJ1775: [Usaco2009 Dec]Vidgame 电视游戏问题
n<=50个游戏机有花费,每个游戏机有Gi<=10种游戏,每种游戏有花费有收益,买了游戏机才能玩对应游戏,求最大收益. 这就是一个背包!不过有依存关系,就不会了! 方法一:f[i][j]表 ...
- HDU 4651 (生成函数)
HDU 4651 Partition Problem : n的整数划分方案数.(n <= 100008) Solution : 参考资料: 五角数 欧拉函数 五边形数定理 整数划分 一份详细的题 ...