简体   繁体   中英

Compare elements of two lists in java

I have two lists where some information is stored. Elements of first list are extracted from a DB (they are the ingredients of recipes), in the second list some favourite ingredients are stored. I want to compare both lists and when the ingredients match I want to add 1 to a sum. My problem is when I parse the first list the ingredients are compared with the second list, but in my first list I have elements that are not made from one string( one element has 2-3 words), and when I compare the lists, are compared only the last elements, aren't compared the 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 my result is

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

We can saw that word "mere" is met twice in first list an once in second list,but the result is 0. How can I resolve the problem?

Here is my 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 from ingredientplacut where Id_user = '24'");


 while( rs1.next()){
   String nrRet1 = rs1.getString("Nume");
   secondList.add(nrRet1);                                
   }

   System.out.println(secondList);      
   ArrayList<String> al3= new ArrayList<String>();
   for (String temp : firstList)
   al3.add(secondList.contains(temp) ? "Yes" : "No");
   System.out.println(al3);


   ArrayList<Integer> al4= new ArrayList<Integer>();
   for (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 My result is [0, 0, 0, 0, 0, 0, 0, 0, 0] instead of [0, 0, 0, 0, 0, 0, 0, 0, 1]

Could anyone help me? Thanks!

You can simple add an Inner for loop to check the content.Your problem is an element from List2 can contain inside an element of list1 rather than being exactly same.For eg list1 has "200 grame fusilli cu legume (spanac si mere)" and list 2 has only "mere".This can be solved by adding another inner for loop.

See the below small code here I have fixed the contents of " al3 ".U can do the same for al4

 for (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 your input sets code for al4 will be :

for (String temp2 : secondList) {
        boolean isTrue = false;
        for (String temp : firstList) {
            if (temp.contains(temp2)) {
                isTrue = true;
                break;
            }
        }
        if (isTrue)
            al4.add(1);
        else
            al4.add(0);
    }

As list2 element can be inside list1 element I have customized the code accordingly.You can customize your code according to input patterns.

Have you looked at using CollectionUtils.intersection ?

This will perform a match against two sets (you may want to look at splitting your strings from the first set) and give you all elements that exist in both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM