1

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!!
S.O.S
  • 848
  • 10
  • 30

2 Answers2

0

Collections.disjoint() returns true since the first List - text1List - contains String elements and the second List - comparisonTextQuoteListOfLists - contains List<String> elements. Hence the two Lists 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!!");
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Given the output: `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!` It produces `NO MATCH!!` even though `text2ListOfLists` contains: `key12 key13 key14 key15 key16` and `key13-key17 -> **MATCH!!** If any entry in List: `textList1` is present as entry in **any sub list** of `text2ListOfLists` = true TX – S.O.S Feb 03 '20 at 07:12
  • @S.O.S is that the output of your original code or of my suggested code? If it's the output of my code, please edit your question with the full declaration and initialization of all the Lists, so that I can check it myself. – Eran Feb 03 '20 at 07:16
  • updated OP with your code + output. As mentioned, I was expecting MATCH!! instead of NO MATCH!! – S.O.S Feb 03 '20 at 07:23
  • @S.O.S I don't know what `StringX.splitStrIntoWordChunks()` is, so I can't test it. Can you replace those calls with assignment of the actual Lists returned by those calls? – Eran Feb 03 '20 at 07:25
  • `StringX.splitStrIntoWordChunks()` is an exact replica of the following code I posted on SO: https://stackoverflow.com/a/60033687/8814573 Please let me know if you want me to post more code.. Thanks! – S.O.S Feb 03 '20 at 07:27
  • @S.O.S The method in the link also has a call to `StringX.splitStrIntoWordsRtrnArr`. – Eran Feb 03 '20 at 07:31
  • that just splits String into individual words via regex str.split(" "); – S.O.S Feb 03 '20 at 07:32
  • The call to `splitStrIntoWordChunks()` gives the following output: `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]] comparisonTextQuoteListOfLists = [[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]]` – S.O.S Feb 03 '20 at 07:33
  • @S.O.S why are the Lists in your original post (before the edits) different from the current Lists? For the current Lists, `contains` returns `false`, since `comparisonTextQuoteListOfLists` doesn't contain any List which is equal to `text1List`. – Eran Feb 03 '20 at 07:40
  • thanks for your help I added my own solution as answer.. it provides the functionality I need.. – S.O.S Feb 03 '20 at 17:04
0

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.

S.O.S
  • 848
  • 10
  • 30