Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象
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}
{1=org.rui.collection2.map.Pair@170bea5, 2=org.rui.collection2.map.Pair@f47396, 3=org.rui.collection2.map.Pair@d0af9b, 4=org.rui.collection2.map.Pair@b8f8eb, 5=org.rui.collection2.map.Pair@1de17f4, 6=org.rui.collection2.map.Pair@1f6ba0f, 7=org.rui.collection2.map.Pair@1313906, 8=org.rui.collection2.map.Pair@96cf11}
{a=value, b=value, c=value, d=value, e=value}
*/
Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象的更多相关文章
- Java集合(2):两个生成器的例子:Collection生成器CollectionData及Map生成器MapData
Collection生成器CollectionData CollectionData体现了适配器模式的设计思想,它能把实现Generator接口的类的对象(包括上一章数组中的各种RandomGener ...
- 从头认识java-15.1 填充容器(3)-填充Map
这一章节我们来讨论一下填充容器的还有一个方面Map.之前的两个章节我们都是用list来作为容器.这一章节我们使用Map. 还有在这里解释一下为什么一直都使用生成器这个东西,事实上他就是建造者设计模式, ...
- Geometric Progression---cf 567C(求组合方式,map离散)
题目链接:http://codeforces.com/contest/567/problem/C 题意就是有n个数现在要让 ai aj ak 构成公比为K的等比数列(i < j < k) ...
- Java编程思想之十七 容器深入研究
17.1 完整的容器分类方法 17.2 填充容器 import java.util.*; class StringAddress { private String s; public StringAd ...
- Map<k,v>接口
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html public interface Map<K,V> K—key,V ...
- python迭代器、生成器、装饰器
1 迭代器 这里我们先来回顾一下什么是可迭代对象(Iterable)? 可以直接作用于for循环的对象统称为可迭代对象,即Iterable. # 一是集合数据类型,如list.tuple.dict.s ...
- [JS] ECMAScript 6 - Array : compare with c#
扩展运算符(spread) 先复习下 rest 参数. (1) argument模式,但不够好. // https://blog.csdn.net/weixin_39723544/article/de ...
- js面试相关
〇,字符串,数值,数组的转化 (0)检测数据类型 参考连接:http://www.cnblogs.com/onepixel/p/5126046.html 1,, typeof 操作符 : 能检测到( ...
- 介绍一个golang库:fastcache
学习VictoriaMetrics源码的时候发现,VictoriaMetrics的缓存部分,使用了同一产品下的fastcache.下面分享阅读fastcache源码的的结论: 1.官方介绍 fastc ...
随机推荐
- DOM笔记(十):JavaScript正则表达式
一.RegExp ECMAScript通过RegExp类型类支持正则表达式,语法和Perl类似: var exp = /pattern/flags; patternb部分是任何简单的或复杂的正则表达式 ...
- DOM笔记(六):怎么进行JQuery扩展?
一.全局函数的扩展 全局函数是将独立的函数添加到JQuery的命名空间中区.在使用的时候,可以通过$.fucnName(param)或者jQuery.funcName(param)方式进行调用. 1. ...
- 【Android】创建、读取XML文件
创建: package webdomain; import java.io.File; import java.io.FileNotFoundException; import java.io.Fil ...
- 耳机jack构造及在应用时可能出现的问题
目前市场上耳机分为4环耳机(图1所示,iphone型)和3环耳机(图2所示).4环耳机称为headset,3环耳机称为headphone,两者之间的区别就是4环耳机比3环耳机多个micphone.而J ...
- Cocos2d-x项目移植到WinRT/Win8小记
Cocos2d-x项目移植到WinRT/Win8小记 作者: K.C. 日期: 11/17/2013 Date: 2013-11-17 23:33 Title: Cocos2d-x项目移植到WinRT ...
- Options for Debugging Your Program or GCC
[Options for Debugging Your Program or GCC] -g Produce debugging information in the operating system ...
- HDU 4617 Weapon (简单三维计算几何,异面直线距离)
Weapon Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- 安装Sass
最近要开始用 Sass 做一些东西.先来记录一下安装过程. 1.确认本机的 Ruby 版本 2.访问网址下载 Sass 最新版本 https://rubygems.org/gems/sass 3.下载 ...
- 程序设计第三次作业--C++计算器初始部分
面向对象程序设计作业3--C++计算器初始部分 Github 链接:https://github.com/luojingzhao/object-oriented/tree/master/calcula ...
- manacher算法(转载)
原网址:http://blog.sina.com.cn/s/blog_70811e1a01014esn.html manacher算法是我在网上无意中找到的,主要是用来求某个字符串的最长回文子串.不过 ...