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

來源:互聯網
上載者:User

標籤:為什麼   count   目的   靈活   ons   stat   包含   word   com   

這一章節我們來討論一下填充容器的還有一個方面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

輸出:

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;@Overridepublic 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).";@Overridepublic 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.