Java Dual loop

Source: Internet
Author: User

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

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.