In the actual development we often encounter such problems, there are a, B two sets, the two sets of a field is the same, to match a set and B, and then assign a value to B for example:
Upload image
List<multipartfile> a=multipartrequest.getfiles ("FileName");
List<multipartfile> c=new arraylist<multipartfile> ();
List<tquestioninfo> b=questioninfoservice.gettasklist (Taskuser.gettaskcode ());
for (Tquestioninfo q:b) {
for (Multipartfile t:a) {
if (Q.getquestionnum (). Equals (T.getoriginalfilename ())) {
C.add (t);
}
Q.setttaskquestionuserpic (C);
}
That's right, but there's a very serious problem with this, so the loop is n*n, meaning that if a has 10,000 data B with 10,000 data then the loop loops 100 million times, and if A and B have 100,000 data then I can't imagine.
In this case I chose to encapsulate a collection, for example, to encapsulate a set as map <String,<MultipartFile>> and then loop B to take the same field as the key value for example:
Package as Map
Public map<string,list<ttaskquestionuserpic>> Gettaskquetionusermappic (list<ttaskquestionuserpic > list) {
map<string,list<ttaskquestionuserpic>> map = new hashmap<string, list<ttaskquestionuserpic> > ();
List<ttaskquestionuserpic> Childlist=null;
Iterator<ttaskquestionuserpic> It=list.iterator ();
Ttaskquestionuserpic info =null;
while (It.hasnext ()) {
Info=it.next ();
Childlist=map.get (Info.gettaskquestionusercode ());
if (childlist==null) {
Childlist=new arraylist<ttaskquestionuserpic> ();
Map.put (Info.gettaskquestionusercode (), childlist);
}
Childlist.add (info);
}
return map;
}
Remove the value from the map
for (Ttaskquestionuser q:questionuserlist) {
List<ttaskquestionuserpic> list= Map.get (Q.gettaskquestionusercode ());
if (Emptyutils.isnotempty (list))
Q.setpiclist (list);
Tquestioninfo Questioninfo=getquestionmap.get (Q.getquestionnum ());
Q.setstem (Questioninfo.getstem ());
Q.setquestionanalyze (Questioninfo.getquestionanalyze ());
}
Taskuser.setquestionuserlist (questionuserlist);
This may seem tedious but the most important point is that the efficiency of its execution is much higher than that of a direct loop of two sets. Because it's just a loop of two sets, which means a and B each have 10,000 data then he only executes 20,000 times instead of the 100 million times of the direct loop, which is much higher than the efficiency of the direct loop!
Java Dual loop