Learn from the java-15.1 filling container (3)-Fill map

Source: Internet
Author: User

In this chapter we will discuss the other aspects of the fill container map. In the previous two chapters we used a list as a container. In this chapter we use map.

And here's an explanation of why the generator is used all the time, in fact he is the builder design pattern, and its basic function is to produce complex objects and to meet the changing needs (flexibility).

And why spend so many chapters discussing filling containers, mainly because the fill container contains more knowledge points, the knowledge points are enumerated:

(1) Generic type

(2) Builder design pattern

(3) The Fill method for the container (list's Add. Map's put, etc.)

To get to the topic, let's discuss the map's fill

1. Examples

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

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. Due to the need to populate Map,pair's key and value we are all labeled final. Used in this way.

(3) Lettergenerator realizes the generator, then assembles the desired object into a pair

(4) Mymap inherits HashMap, expands new constructor

(5) through the Putall or Collections.addall method inside the map. will be able to produce a new map.


2. Let's change the example above, transform the Mymap constructor (the constructor here can be put together, but put together the code is longer, so we transform the constructor.) Instead of adding it above). To meet a variety of needs.

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 makes it possible to generate a set of objects through 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.

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

Folder



Learn from the java-15.1 filling container (3)-Fill map

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.