繁体   English   中英

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

[英]Check If List of Lists Contains Any Sub List Matching 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!!");
    }

}

输出:

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

但是,由于text2ListOfLists包含来自text1List条目,我希望它打印MATCH !!

如何检查列表列表是否包含具有与列表中的条目匹配的元素的子列表?

如果text2ListOfLists包含字符串: key1 key2 key3 key4 key5 OR key2 key3 key4 key5 key6 OR key3 key4 key5 key6 key7 (它确实如此)它应该返回true..

谢谢!

更新:

以下是@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!!");
        }


    }

输出:

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()返回 true,因为第一个List - text1List - 包含String元素,第二个List - compareTextQuoteListOfLists - 包含List<String>元素。 因此,两个List没有共同的元素。

看起来你应该使用:

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

下面的代码提供了我需要的功能:

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;

}

如果有人可以通过流或更优雅的方式提供相同的功能,我会接受它作为答案。

暂无
暂无

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

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