李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
容器深入研究--Map生成器
Leefs
2020-01-07 AM
1997℃
0条
# 容器深入研究--Map生成器 ### 前言 本篇讲述《Java编程思想》第17.2.2小节,Map生成器 ### 概述 为了组装Map,每次调用Generator的next()方法都必须产生一个对象对(一个键和一个值): ```java public class Pair
{ public final K key; public final V value; public Pair(K k, V v) { key = k; value = v; } } ``` key和value域都是public和final的,这是为了使Pair成为只读的数据传输对象。 **Map适配器使用Generator、Iterator和常量值的组合来填充Map初始化对象:** ```java public class MapData
extends LinkedHashMap
{ // A single Pair Generator: public MapData(Generator
> gen, int quantity) { for(int i = 0; i < quantity; i++) { Pair
p = gen.next(); put(p.key, p.value); } } // Two separate Generators: public MapData(Generator
genK, Generator
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
genK, V value, int quantity){ for(int i = 0; i < quantity; i++) { put(genK.next(), value); } } // An Iterable and a value Generator: public MapData(Iterable
genK, Generator
genV) { for(K key : genK) { put(key, genV.next()); } } // An Iterable and a single value: public MapData(Iterable
genK, V value) { for(K key : genK) { put(key, value); } } // Generic convenience methods: public static
MapData
map(Generator
> gen, int quantity) { return new MapData
(gen, quantity); } public static
MapData
map(Generator
genK, Generator
genV, int quantity) { return new MapData
(genK, genV, quantity); } public static
MapData
map(Generator
genK, V value, int quantity) { return new MapData
(genK, value, quantity); } public static
MapData
map(Iterable
genK, Generator
genV) { return new MapData
(genK, genV); } public static
MapData
map(Iterable
genK, V value) { return new MapData
(genK, value); } } ``` > 代码分析 1. 1.此类继承LinkedHashMap父类,拥有父类的方法 2. 2.有五个不同参数类型的构造方法,分别以不同的方式初始化,生成对象。 + (1)A single Pair Generator + (2)Two separate Generators + (3)A key Generator and a single value + (4)An Iterable and a value Generator + (5)An Iterable and a single value **MapData.map()方法测试** ```java class Letters implements Generator
>, Iterable
{ private int size = 9; private int number = 1; private char letter = 'A'; public Pair
next() { return new Pair
(number++, "" + letter++); } public Iterator
iterator() { return new Iterator
() { 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) { // Pair Generator: System.out.print(MapData.map(new Letters(), 11)); // Two separate generators: System.out.print(MapData.map(new CountingGenerator.Character(), new RandomGenerator.String(3), 8)); // A key Generator and a single value: System.out.print(MapData.map(new CountingGenerator.Character(), "Value", 6)); // An Iterable and a value Generator: System.out.print(MapData.map(new Letters(), new RandomGenerator.String(3))); // An Iterable and a single value: System.out.print(MapData.map(new Letters(), "Pop")); } } ``` > 运行结果 ```java {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K}{a=YNz, b=brn, c=yGc, d=FOW, e=ZnT, f=cQr, g=Gse, h=GZM}{a=Value, b=Value, c=Value, d=Value, e=Value, f=Value}{1=mJM, 2=RoE, 3=suE, 4=cUO, 5=neO, 6=EdL, 7=smw, 8=HLG}{1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop} ``` 示例中,可以使用工具来创建任何用于Map或Collection的生成数据集,然后通过构造器或Map.putAll()和Collection.addAll()方法来初始化Map和Collection。 **示例中所用到的工具类** **CountingGenerator.java** ```java public class CountingGenerator { public static class Boolean implements Generator
{ private boolean value = false; public java.lang.Boolean next() { value = !value; // Just flips back and forth return value; } } public static class Byte implements Generator
{ private byte value = 0; public java.lang.Byte next() { return value++; } } static char[] chars = ("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); public static class Character implements Generator
{ int index = -1; public java.lang.Character next() { index = (index + 1) % chars.length; return chars[index]; } } public static class String implements Generator
{ private int length = 7; Generator
cg = new Character(); public String() { } public String(int length) { this.length = length; } public java.lang.String next() { char[] buf = new char[length]; for (int i = 0; i < length; i++) buf[i] = cg.next(); return new java.lang.String(buf); } } public static class Short implements Generator
{ private short value = 0; public java.lang.Short next() { return value++; } } public static class Integer implements Generator
{ private int value = 0; public java.lang.Integer next() { return value++; } } public static class Long implements Generator
{ private long value = 0; public java.lang.Long next() { return value++; } } public static class Float implements Generator
{ private float value = 0; public java.lang.Float next() { float result = value; value += 1.0; return result; } } public static class Double implements Generator
{ private double value = 0.0; public java.lang.Double next() { double result = value; value += 1.0; return result; } } } ``` **RandomGenerator.java** ```java public class RandomGenerator { private static Random r = new Random(47); public static class Boolean implements Generator
{ public java.lang.Boolean next() { return r.nextBoolean(); } } public static class Byte implements Generator
{ public java.lang.Byte next() { return (byte) r.nextInt(); } } public static class Character implements Generator
{ public java.lang.Character next() { return CountingGenerator.chars[r .nextInt(CountingGenerator.chars.length)]; } } public static class String extends CountingGenerator.String { // Plug in the random Character generator: { cg = new Character(); } // Instance initializer public String() { } public String(int length) { super(length); } } public static class Short implements Generator
{ public java.lang.Short next() { return (short) r.nextInt(); } } public static class Integer implements Generator
{ private int mod = 10000; public Integer() { } public Integer(int modulo) { mod = modulo; } public java.lang.Integer next() { return r.nextInt(mod); } } public static class Long implements Generator
{ private int mod = 10000; public Long() { } public Long(int modulo) { mod = modulo; } public java.lang.Long next() { return new java.lang.Long(r.nextInt(mod)); } } public static class Float implements Generator
{ public java.lang.Float next() { // Trim all but the first two decimal places: int trimmed = Math.round(r.nextFloat() * 100); return ((float) trimmed) / 100; } } public static class Double implements Generator
{ public java.lang.Double next() { long trimmed = Math.round(r.nextDouble() * 100); return ((double) trimmed) / 100; } } } ```
标签:
Java
,
Java编程思想
,
容器深入研究
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/391.html
上一篇
填充容器--一种Generator解决方案
下一篇
【转载】设计模式之享元模式
取消回复
评论啦~
提交评论
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
标签云
排序
数据结构和算法
栈
Hive
Ubuntu
Sentinel
Flume
工具
JavaSE
并发线程
SpringCloud
Spark Streaming
Spark Core
Java阻塞队列
Eclipse
JavaWeb
Golang
Jenkins
SQL练习题
Java编程思想
Map
并发编程
Docker
容器深入研究
Http
算法
Filter
FileBeat
Kafka
Quartz
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞