As a young intern in the company , I encountered a problem when making a small function of a module :
For example, using sql The following data were found in the library

itemCoderegion
1001 Wuhan
1001 Wuhan
1001 Wuhan
1002 Qingshan District
1002 Wuhan
1002 Jiangxia District
1002 Caidian District
1003 Wuchang District
1003 Dongxihu District
1003 Economic Development Zone
1003 Wuhan
……
The current business needs are :
In the same itemCode Lower if region If there is only one city , This indicates that this function is a municipal function
In the same itemCode Lower if region If there is only zone , This indicates that this function is a district level function
Other cases are urban level functions
Then count the cities separately , area , How many items are there in the urban area
The current situation is that there are more than one million pieces of data found in the database
And my handling was rough , Direct double layer for Sub group on circular violence
I'll take all the itemCode All the same data is divided into one group
This is probably the case ( Don't scold , I'm really stupid )
Data is via ArrayList<String[]> To the business layer , In the first position of the array itemCode, In the second position Region

In this case , More than one million pieces of data is to execute one million * million , Directly to the server. It's stuck . This is not the most exaggerated , I even modify the data I traverse while traversing , Do concurrent modification exceptions no longer exist in my eyesight
Then the project leader asked me to optimize this area again , So we Xiaobai can't , If you think about it, you won't be able to program for Baidu , And then the big brother in the help group , Big brother gave us a solution :
int shi = 0; int qu = 0; int shiqu = 0; int allTatal = 0; ArrayList<String[]>
allPcJieruData= DAO Data received by layer ; Map<String, ArrayList<String>> map = new HashMap<
String, ArrayList<String>>(); for (int i = 0; i < allPcJieruData.size(); i++) {
// grouping String itemCode = allPcJieruData.get(i)[0]; String region = allPcJieruData.
get(i)[1]; ArrayList<String> groupList = map.get(itemCode); if (groupList ==
null) { groupList = new ArrayList<>(); } groupList.add(region); map.put(itemCode
, groupList); } allTatal = map.size(); Iterator<Map.Entry<String, ArrayList<
String>>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<
String, ArrayList<String>> entry = it.next(); ArrayList<String> allist = entry.
getValue(); Boolean isShi = false; Boolean isQu = false; for (int i = 0; i <
allist.size(); i++) { // If Wuhan is the city // If there are cities and districts, they are urban areas // Zone if only zone String regions =
allist.get(i); if (regions.equals(" Wuhan ")) {// Once the group contains cities , On isShi = true; } else {
// Otherwise, it means that the group contains an area , Illuminated area isQu = true; } } if (isShi && isQu) {// If there are municipal and district shiqu++; }
else if (!isShi && isQu) {// Zone if only zone qu++; } else if (isShi && !isQu) {
// If Wuhan is the city shi++; } }
Although the solution given by big brother may not be as good as the one you are looking at , But at least it's much better than mine , The main thing is to avoid double decking for Loop and my pre stored concurrent modification exceptions

Record growth , Common progress , If you have a better plan, I hope you can give me some advice

Technology