This article introduces the features of List and Map in Java to match large batches of data.
A problem occurs in the project: a large volume of data sent over http (this data is conservatively estimated to be about 0.1 million requests each time ), to match another batch of data in the database (about 10 thousand records in the database) (matching: the values of some fields in the two groups of data are equal ), the matching data is stored in the database, and the matching data cannot be directly discarded. Or: there is a List <String> strList, List <Person> personList, strNoList. the size is 10 thousand, personList. the size is 0.1 million, and then the person id attribute of the person in the strList should be retrieved from the personList. The person id in the personList may be the same, and the structures of the two records are different.
To implement this function, the first thought is to use the for loop for comparison one by one, so there will be 1 billion X, that is, cycles. However, the system has a high requirement on real-time data. This obviously causes performance problems. Therefore, we need to find another way to reduce the number of loops to increase the matching processing speed. Because we didn't do this before, we wanted to find a variety of methods and post help in the OSC community.
List can put duplicate data, while Map is the key-value structure that cannot put duplicate data. Then, you can put the person entity data with the same received id into a list, and then use this id as the key and list as the value into the map. Now we need to process 10 million data records for 10 million for loops. Then we can find records in the database, traverse the map, and use map. get ("key") extracts the list with the same id, and then adds all the elements of the list to a resultList. It takes for loops to traverse the records. In this way, a 10 W x 1 W for loop is reduced to 10 W + 1 W. The following is a time-consuming test on the number of for loops. The results show that reducing the number of cycles can greatly increase the processing speed.
Example
| The Code is as follows: |
Copy code |
|
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
/**
* For loop testing
* @ Author desert
*
*/
Public class ForTest {
Public static void main (String [] args ){
ForTest test = new ForTest ();
System. out. println ("================ start ================= ");
// 0.1 billion for Loops
Test. test1Yi ();
// 0.11 million for Loops
Test. test11W ();
// Nested for loop matching: 10 W x 1 W for Loop
Test. testForAndFor ();
// Map and List matching: 10 W + 1 W for Loop
Test. testMapAndList ();
System. out. println ("================ end ============== ");
}
/**
* 0.1 billion for Loops
*/
Private void test1Yi (){
Long start = System. currentTimeMillis ();
For (Integer I = 0; I <100000000; I ++ ){
System. out. println (I );
}
Long end = System. currentTimeMillis ();
System. out. println ("0.1 billion cycles:" + (end-start) + "millisecond ");
System. out. println ("----------------------------");
}
/**
* 0.11 million for Loops
*/
Private void test11W (){
Long start = System. currentTimeMillis ();
For (Integer I = 0; I <110000; I ++ ){
System. out. println (I );
}
Long end = System. currentTimeMillis ();
System. out. println ("cycles:" + (end-start) + "millisecond ");
System. out. println ("----------------------------");
}
/**
* Nested for loop comparison and matching
*/
Private void testForAndFor (){
// Construct a list of 0.1 million Person objects
List <Person> personList = new ArrayList <Person> ();
For (int I = 0; I <100000; I ++ ){
Int j = 10000;
PersonList. add (new Person (I % j) + "", "Michael" + I ));
}
// Construct a list of 10 thousand String objects
List <String> strList = new ArrayList <String> ();
For (int I = 0; I <10000; I ++ ){
StrList. add (I + "");
}
// Nested for loop: 10 W * 1 W
Long start = System. currentTimeMillis ();
// Save the matching result
List <Person> resultList = new ArrayList <Person> ();
// Traverse the List of 10 W persons
For (Person person: personList ){
// Traverse the List of 1 W str
For (String str: strList ){
If (person. getId (). equals (str )){
ResultList. add (person );
}
}
}
Long end = System. currentTimeMillis ();
System. out. println ("reslutList. size:" + resultList. size ());
System. out. println ("10 W x 1 W cycle time:" + (end-start) + "millisecond ");
System. out. println ("----------------------------");
}
/**
* Use the Map and List features for matching:
* Map is a key-value structure, and duplicate data cannot be stored.
* List can store duplicate data.
* The String id is used as the key, and the List <Person> is used as the value.
* Traverse the List <String>, and map. get (String) retrieves the List with id = str.
*/
Private void testMapAndList (){
// Construct a list of 0.1 million Person objects
List <Person> personList = new ArrayList <Person> ();
For (int I = 0; I <100000; I ++ ){
Int j = 10000;
PersonList. add (new Person (I % j) + "", "Michael" + I ));
}
// Construct a list of 10 thousand String objects
List <String> strList = new ArrayList <String> ();
For (int I = 0; I <10000; I ++ ){
StrList. add (I + "");
}
Long start = System. currentTimeMillis ();
// Use the Map and List features to sort data
Map <String, List <Person> map = new HashMap <String, List <Person> ();
// Add 10 million pieces of data to map by id
For (int I = 0; I <personList. size (); I ++ ){
Person person = personList. get (I );
String id = person. getId ();
If (map. containsKey (id )){
Map. get (id). add (person );
} Else {
List <Person> pList = new ArrayList <Person> ();
PList. add (person );
// The id is the key, and the List of persons with the same id is the value
Map. put (id, pList );
}
}
// Save the matching result
List <Person> resultList = new ArrayList <Person> ();
// Retrieve matching data based on 1 W str and map. get (str) records
For (String str: strList ){
List <Person> pList = map. get (str );
If (pList! = Null ){
ResultList. addAll (pList );
}
}
Long end = System. currentTimeMillis ();
System. out. println ("map. size:" + map. size ());
System. out. println ("reslutList. size:" + resultList. size ());
System. out. println ("10 W + 1 W cycle time:" + (end-start) + "millisecond ");
System. out. println ("----------------------------");
}
}
/**
* Person entity class
*/
Class Person {
Private String id;
Private String name;
Public Person (){}
Public Person (String id, String name ){
This. id = id;
This. name = name;
}
@ Override
Public String toString (){
Return this. id + ":>" + this. name;
}
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
} |
Test results:
================ Start =====================
0.1 billion cycles: 1262985 milliseconds
----------------------------
1016 cycles: milliseconds
----------------------------
ReslutList. size: 100000
21219 * cycles: milliseconds
----------------------------
Map. size: 10000
ReslutList. size: 100000
+ Cycles: 31 Ms
========================================
• 0.1 billion system. out. println (I) cycles took 1262985 milliseconds, that is, 21 minutes, 1 billion times, 210 minutes, obviously unacceptable. Of course, I/O operations are designed here, which is time-consuming and not so scary in actual applications.
• 0.11 million system. out. println (I) cycles take 1016 milliseconds, that is, one second. Obviously, reducing the number of cycles can increase the processing speed.
• It Takes 21219 milliseconds to use a nested for loop to complete 10 W * 1W cycles. The second method is used to complete 10 W + 1W cycles, which takes 31 ms and improves the processing speed by more than 600, achieve the expected purpose.