简体   繁体   English

检查列表列表是否包含列表中的任何子列表匹配元素

[英]Check If List of Lists Contains Any Sub List Matching Element from List

As recommended here , I am using the following code to check if List<List<String>> contains any sub list matching any element from List .按照此处的建议,我使用以下代码检查List<List<String>>包含与List任何元素匹配的任何子List

for (List<String> text1List : text2ListOfLists) {

    System.out.println("text1List = " + text1List.toString() + " text2ListOfLists = " + comparisonTextQuoteListOfLists.toString());

    if (!Collections.disjoint(text1List, comparisonTextQuoteListOfLists))
    {
        System.out.println("MATCHES!!");
    } else {
        System.out.println("NO MATCH!!");
    }

}

OUTPUT:输出:

text1List = [key1 key2 key3 key4 key5, key2 key3 key4 key5 key6, key3 key4 key5 key6 key7] text2ListOfLists = [[key1 key2 key3 key4 key5, key2 key3 key4 key5 key6, key3 key4 key5 key6 key7], [key11 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17]]
NO MATCH!!
text1List = [key11 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17] text2ListOfLists = [[key1 key2 key3 key4 key5, key2 key3 key4 key5 key6, key3 key4 key5 key6 key7], [key11 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17]]
NO MATCH!!

However, since text2ListOfLists contains entries from text1List I would expect this to print MATCH!!但是,由于text2ListOfLists包含来自text1List条目,我希望它打印MATCH !!

How can I check if a list of lists contains a sub list with entry matching element from list?如何检查列表列表是否包含具有与列表中的条目匹配的元素的子列表?

If text2ListOfLists contains the String: key1 key2 key3 key4 key5 OR key2 key3 key4 key5 key6 OR key3 key4 key5 key6 key7 (which it does) it should return true..如果text2ListOfLists包含字符串: key1 key2 key3 key4 key5 OR key2 key3 key4 key5 key6 OR key3 key4 key5 key6 key7 (它确实如此)它应该返回true..

Thanks!谢谢!

Update:更新:

Below is updated code + output of @Eran code:以下是@Eran代码的更新代码+输出:

    final ArrayList<String> textWords = new ArrayList<String>();

    textWords.add("key1 key2 key3 key4 key5 key6 key7");
    textWords.add("key11 key12 key13 key14 key15 key16 key17");

    final ArrayList<String> textWords1 = new ArrayList<String>(); 

    textWords1.add("key111 key112 key113 key114 key115 key116 key117");
    textWords1.add("key110 key12 key13 key14 key15 key16 key17");

    int desiredListSize = 5;

    List<List<String>> text2ListOfLists = StringX.splitStrIntoWordChunks(textWords, desiredListSize);



    List<List<String>> comparisonTextQuoteListOfLists = StringX.splitStrIntoWordChunks(textWords1, desiredListSize);




    for (List<String> text1List : text2ListOfLists) {

        System.out.println("textList1 = " + text1List.toString() + " text2ListOfLists = " + comparisonTextQuoteListOfLists.toString());


        if (comparisonTextQuoteListOfLists.contains(text1List))
        {
            System.out.println("MATCH!!");
        } else {
            System.out.println("NO MATCH!!");
        }


    }

OUTPUT:输出:

textList1 = [key1 key2 key3 key4 key5, key2 key3 key4 key5 key6, key3 key4 key5 key6 key7] text2ListOfLists = [[key111 key112 key113 key114 key115, key112 key113 key114 key115 key116, key113 key114 key115 key116 key117], [key110 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17]]
NO MATCH!!
textList1 = [key11 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17] text2ListOfLists = [[key111 key112 key113 key114 key115, key112 key113 key114 key115 key116, key113 key114 key115 key116 key117], [key110 key12 key13 key14 key15, key12 key13 key14 key15 key16, key13 key14 key15 key16 key17]]
NO MATCH!!

Collections.disjoint() returns true since the first List - text1List - contains String elements and the second List - comparisonTextQuoteListOfLists - contains List<String> elements. Collections.disjoint()返回 true,因为第一个List - text1List - 包含String元素,第二个List - compareTextQuoteListOfLists - 包含List<String>元素。 Hence the two List s have no common elements.因此,两个List没有共同的元素。

It looks like you should be using:看起来你应该使用:

if (comparisonTextQuoteListOfLists.contains(text1List)) {
    System.out.println("MATCHES!!");
} else {
    System.out.println("NO MATCH!!");
}

Code below provides the functionality I need:下面的代码提供了我需要的功能:

public static boolean containsEntry(List<String> text1List, List<List<String>> listOfLists)
{

    for (List<String> listFromListOffLists: listOfLists) {

        for (String entryFromList : listFromListOffLists) {

            if (text1List.contains(entryFromList)) {

                return true;
            }
        }

    }


    return false;

}

If anyone can provide same functionality via stream or more elegant fasion I will accept it as the answer.如果有人可以通过流或更优雅的方式提供相同的功能,我会接受它作为答案。

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

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