從頭認識java-15.1 填充容器(3)-填充Map

來源:互聯網
上載者:User

從頭認識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 myMap = new MyMap(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 next();}class LetterGenerator implements Generator> {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;@Overridepublic Pair next() {int param = new Random().nextInt(index);return new Pair(param, str.split(" ")[param]);}}class Pair {public final K key;public final V value;public Pair(K key, V value) {this.key = key;this.value = value;}}@SuppressWarnings("serial")class MyMap extends HashMap {public MyMap(Generator> 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 myMap = new MyMap(new KeyGenerator(), new ValueGenerator(), 10);for (Integer key : myMap.keySet()) {System.out.println("key:" + key + " value:" + myMap.get(key));}new HashMap().putAll(myMap);// 這樣就可以通過putAll產生一組對象。}}interface Generator {T next();}class KeyGenerator implements Generator {private Integer index = 10;@Overridepublic Integer next() {return new Random().nextInt(index);}}class ValueGenerator implements Generator {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).";@Overridepublic String next() {return str.split(" ")[new Random().nextInt(str.split(" ").length - 1)];}}@SuppressWarnings("serial")class MyMap extends HashMap {public MyMap(Generator keyGenerator, Generator 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的填充。

這一章節就到這裡,謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.