简体   繁体   English

比较java中两个列表的元素

[英]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.我想比较两个列表,当成分匹配时,我想在总和上加 1。 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.我的问题是当我解析第一个列表时,将成分与第二个列表进行比较,但是在我的第一个列表中,我的元素不是由一个字符串组成的(一个元素有 2-3 个单词),当我比较列表时,只比较最后一个元素,不比较元素的组件。

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?我们可以看到“mere”这个词在第一个列表中遇到了两次,在第二个列表中遇到了一次,但结果是 0。我该如何解决这个问题?

Here is my java code:这是我的Java代码:

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]我的结果是[0, 0, 0, 0, 0, 0, 0, 0, 0]而不是[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.您可以简单地添加一个内部 for 循环来检查内容。您的问题是List2 中的一个元素可以包含在list1的元素内而不是完全相同。例如,list1 有“200 克 fusilli cu 豆科植物(spanac si mere)”和列表 2 只有“仅仅”。这可以通过添加另一个内部 for 循环来解决。

See the below small code here I have fixed the contents of " al3 ".U can do the same for al4看下面的小代码这里我已经修复了“ al3 ”的内容。你可以对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 :对于 al4 的输入集代码将是:

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.由于 list2 元素可以在 list1 元素内,我已经相应地自定义了代码。您可以根据输入模式自定义代码。

Have you looked at using CollectionUtils.intersection ?你看过使用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.这将对两个集合执行匹配(您可能希望查看从第一个集合中拆分您的字符串)并为您提供两者中存在的所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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