![](/img/trans.png)
[英]How to check if the exact elements in an array are contained within an array stored in an ArrayList?
[英]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.