zoukankan      html  css  js  c++  java
  • 从头认识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的填充。

    这一章节就到这里,谢谢。

    -----------------------------------

    文件夹


     


  • 相关阅读:
    learning scala view collection
    scala
    learning scala dependency injection
    learning scala implicit class
    learning scala type alise
    learning scala PartialFunction
    learning scala Function Recursive Tail Call
    learning scala Function Composition andThen
    System.Threading.Interlocked.CompareChange使用
    System.Threading.Monitor的使用
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7140965.html
Copyright © 2011-2022 走看看