Compare elements of two lists in java -


i have 2 lists information stored. elements of first list extracted db (they ingredients of recipes), in second list favourite ingredients stored. want compare both lists , when ingredients match want add 1 sum. problem when parse first list ingredients compared second list, in first list have elements not made 1 string( 1 element has 2-3 words), , when compare lists, compared last elements, aren't compared components of elements.

first list :

[200 grame fusilli cu legume (spanac si mere); 200 grame smantana 12%;  50 grame iaurt;  50 grame cascaval afumat;  1/2 lingurite mustar;  doi catei de usturoi sau o lingurita de usturoi deshidratat;  o lingur? ulei de masline;  mere] 

second list:

[afine, almette, alune, alune, albus de ou de gaina, alune, andive, mere] 

and result

[0, 0, 0, 0, 0, 0, 0, 0, 0] 

we can saw word "mere" met twice in first list once in second list,but result 0. how can resolve problem?

here java code:

rs=ps.executequery();                            while( rs.next()){ string nrret = rs.getstring("ingrediente");           firstlist.add(nrret);                   } system.out.println(firstlist); statement st1 = con.createstatement(); rs1 = st1.executequery("select nume ingredientplacut id_user = '24'");    while( rs1.next()){    string nrret1 = rs1.getstring("nume");    secondlist.add(nrret1);                                    }     system.out.println(secondlist);          arraylist<string> al3= new arraylist<string>();    (string temp : firstlist)    al3.add(secondlist.contains(temp) ? "yes" : "no");    system.out.println(al3);      arraylist<integer> al4= new arraylist<integer>();    (string temp2 : firstlist)    al4.add(secondlist.contains(temp2) ? 1 : 0);    system.out.println(al4); 

my output is:

first list : [200 grame fusilli cu legume (spanac si **mere**); 200 grame smantana 12%;  50 grame iaurt;  50 grame cascaval afumat;  1/2 lingurite mustar;  doi catei de usturoi sau o lingurita de usturoi deshidratat;  o lingur? ulei de masline;  **mere**] second list: [afine, almette, alune, alune, albus de ou de gaina, alune, andive, mere] 

and result [0, 0, 0, 0, 0, 0, 0, 0, 0] instead of [0, 0, 0, 0, 0, 0, 0, 0, 1]

could me? thanks!

you can simple add inner loop check content.your problem element list2 can contain inside element of list1 rather being same.for e.g list1 has "200 grame fusilli cu legume (spanac si mere)" , list 2 has "mere".this can solved adding inner loop.

see below small code here have fixed contents of "al3".u can same al4

 (string temp : firstlist)        {            boolean istrue=false;            for(string temp2:secondlist)            {                if(temp.contains(temp2))                {                    istrue=true;                    break;                }            }            if(istrue)                al3.add("yes");            else                al3.add("no");         } 

for input sets code al4 :

for (string temp2 : secondlist) {         boolean istrue = false;         (string temp : firstlist) {             if (temp.contains(temp2)) {                 istrue = true;                 break;             }         }         if (istrue)             al4.add(1);         else             al4.add(0);     } 

as list2 element can inside list1 element have customized code accordingly.you can customize code according input patterns.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -