简体   繁体   中英

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 .

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!!

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..

Thanks!

Update:

Below is updated code + output of @Eran code:

    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. Hence the two List s have no common elements.

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.

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