In this chapter we discuss another aspect of the fill container map, the previous two chapters we use list as a container, this chapter we used map.
And here's an explanation of why the generator has always been used, but he is the builder design pattern, and its main function is to produce complex objects, and to meet the changing needs of various (flexibility).
And why spend so many chapters discussing filling containers, mainly because the fill container includes more knowledge points, the knowledge points are enumerated:
(1) Generic type
(2) Builder design pattern
(3) How to fill the container (list of add,map put, etc.)
To get to the topic, let's discuss the map's fill
1. Example
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 (), ten); for (Integer Ke Y:mymap.keyset ()) {System.out.println ("key:" + key + "value:" + mymap.get (key));} New HashMap
(). Putall (MYMAP);//This allows you to generate a set of objects through Putall. }}interface generator<t> {T next ();} Class Lettergenerator implements Generator<pair<integer, string>> {private String str = "The PLA Daily must ad Here to the leadership ' + ' of the Communist Party of China (CPC) and serve the PLA, "+" which is also under the CPC leader ship, said Xi., who are "+" also general secretary of the CPC Central Committee and "+" chairman of the central Military Com Mission (CMC). "; Private Integer index = Str.split (""). Length-1; @Overridepublic Pair<integer, string> next () {int param = new Rand Om (). 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 = V Alue;}} @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);}}}
Output:
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
Explain the above code:
(1) Purpose: Generate a set of (numbers, strings) map, numbers and strings are random
(2) We need to assemble the class Pair, because we need to populate the Map,pair key and value we are all labeled final, such aspects of use.
(3) Lettergenerator implement generator, then assemble the desired object into pair
(4) Mymap inherits HashMap, expands new constructor
(5) A new map can be produced using the Putall or Collections.addall method in the map.
2. Let's change the example above, transform the Mymap constructor (where the constructors can be put together, but put together the code will be longer, so we transform the constructor instead of adding it above) to meet a variety of requirements.
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 (), For (Integer Key:myMap.keySet ()) {System.out.println ("key:" + key + "value:" + mymap.get (key));} New Hashmap<integer, String> (). Putall (MYMAP);//This allows you to generate a set of objects by Putall. }}interface generator<t> {T next ();} Class Keygenerator implements generator<integer> {Private integer index = ten; @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 are 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 (), V Aluegenerator.next ());}}}
Output:
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
The above code allows us to separate the pair from the combination class.
Summary: We've covered the map's fill above.
This chapter is here, thank you.
-----------------------------------
Directory
Learn from the java-15.1 filling container (3)-Fill map