繁体   English   中英

如何检查两个列表中的元素是否包含在另一个列表中?

[英]How to check if elements of 2 lists are contained within the other?

我想验证List<String>中包含的每个元素是否包含在第二个列表的任何元素中。

例:

[hotel, appartment]
[hotel, appart]

Analye:

hotel      <> hotel  -> OK  (100% match)
appartment <> hotel  -> NOK 
appartment <> appart -> OK  ("appartment" is not contained in "appart", but the other way around)

<>读取:两者之一都包含在另一个字符串中。

如何最好地做到这一点,尤其是从性能的角度来看? 已经有处理这些情况的Apache Commons或番石榴函数吗?

那是您要找的东西吗? 我没有运行此代码,只是将其写在记事本上...

for(int a=0;a<listA.size();a++){
    for(int b=0;b<listB.size();b++){
        if(listA.get(a).equals(listB.get(b))){
            System.out.println(listA.get(a) + "<>" + listB.get(b) + " -> OK  (100% match)");
        }else{
            //if not match, check if contained
            if(listA.get(a).indexOf(listB.get(b))!=-1 || listB.get(b).indexOf(listA.get(a))!=-1){
                System.out.println(listA.get(a) + "<>" + listB.get(b) + " -> OK  ("appartment" is not contained in "appart", but the other way around)");
            }else{
                System.out.println(listA.get(a) + "<>" + listB.get(b) + " -> NOK ");
            }
        }
    }//for list B
}//for list A

暂无
暂无

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

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