Java-15.1 filling container (3)-filling Map

Source: Internet
Author: User

Java-15.1 filling container (3)-filling Map

In this chapter, we will discuss another aspect of Map filling containers. In the previous two chapters, we use list as the container. In this chapter, we use Map.

I also want to explain why generator is always used. In fact, it is the builder design mode. Its main function is to produce complex objects and meet the changes (flexibility) of various needs ).

Why do we spend so many chapters discussing filling containers? The main reason is that filling containers includes many knowledge points and lists the knowledge points:

(1) Generic

(2) builder Design Model

(3) Container filling method (add of list, put of map, etc)

Go to the topic and discuss Map filling.

1. Example

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); // you can use putAll to generate a group of objects .}} 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 );}}}
           
          
         
        
       
      
     
    
   
  
 

Output:

Key: 1 value: adhere
Key: 32 value: chairman
Key: 2 value:
Key: 21 value: CPC
Key: 23 value: PLA
Key: 22 value:
Key: 25 value: leadership,
Key: 24 value: CPC
Key: 9 value: China
Key: 30 value: serve

Explain the above Code:

(1) Purpose: To generate a set of (numbers, strings) maps, numbers, and strings are random.

(2) We need to assemble the Pair class, because we need to fill in the Map. The keys and values of Pair are marked as final, which is used in this way.

(3) LetterGenerator implements Generator and then assembles the required objects into Pair.

(4) MyMap inherits HashMap and extends the new constructor.

(5) The putAll or Collections. addAll method in the Map can be used to produce a new Map.

2. let's modify the example above to transform the MyMap Constructor (here the constructor can be put together, but the code will be long together, so we have transformed the constructor, instead of adding) to meet various needs.

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); // you can use putAll to generate a group of objects .}} 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 ());}}}
          
         
        
       
      
     
    
   
  
 

Output:

Key: 0 value:
Key: 1 value: CPC
Key: 3 value: Central
Key: 6 value:
Key: 7 value:
Key: 8 value: and
Key: 9 value: under

The above code separates the combined Pair classes for implementation.

Summary: we have introduced the filling of Map.

This chapter is here. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.