[Spring IOC] XML method of injecting collection attributes exercise _ data flow between collections



XML method of injecting collection attributes exercise

Insert picture description here

The above is the basic operation of Spring IOC injecting collection type attributes based on the XML configuration file. Let’s do an exercise~

Please use what you have learned and use Spring to complete the following operations:

  • 1. Store several sets of data in ArrayList (including duplicates, hypotheses 泛型均为String)
  • 2. Finally, the content is output and displayed in the form of Map collection key-value pairs
    • key1 --> xxx
    • key2 --> xxx

Method 1: Compare one by one

  • Create an ArrayList collection to store the data, configure an XML configuration file, take out each element in the test class and add it to the Map collection, and use containsValue()methods to judge at the same time , to discard the existing values, to achieve the goal of deduplication, and finally output the Map collection .
  • This method is simply to traverse the comparison process, the code will not be repeated!

Method 2: The data is traversed and extracted and stored in the set collection, and then stored in the Map collection

Insert picture description here


Method Three: Optimize on the basis of Method Two

Insert picture description here
The advantage of the second method is that the characteristics of the set collection can be used to remove duplication, but the writing method is not simple enough. Because List, Setbelong to Collectionthe set of interfaces, so that the two members can be transformed,List collection can be converted to Set collection, Set collection can also be converted to List collection, So there is no need to take the traversal method, and the Map collection directly belongs to the Map collection interface, so it cannot be converted with the List and Set collections.

In addition, only the original data is retained in the ArrayList, and the following Set and Map collections only exist as tool collections, so there is no need to hand over to Spring for management.


  • Focus:
    • 1. The meaning of Spring management-actually retain important data
    • 2. There is no need to use Spring management for some "tools" in the process-creating intermediate storage
    • 3. The mutual conversion between collections (Collection<List\Set>)-direct construction (omit the amount of code stored in the loop traversal extraction!!!)
    • 4. The traversal method of the collection Map

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/114696048