【问题标题】:Check If List of Lists Contains Any Sub List Matching Element from List检查列表列表是否包含列表中的任何子列表匹配元素
【发布时间】:2020-05-18 22:11:08
【问题描述】:

按照here 的建议,我正在使用以下代码检查List<List<String>> 是否包含与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!!

【问题讨论】:

    标签: java list collections apache-commons


    【解决方案1】:

    Collections.disjoint() 返回 true,因为第一个 List - text1List - 包含 String 元素,第二个 List - comparisonTextQuoteListOfLists - 包含 List&lt;String&gt; 元素。因此这两个Lists 没有共同的元素。

    看起来你应该使用:

    if (comparisonTextQuoteListOfLists.contains(text1List)) {
        System.out.println("MATCHES!!");
    } else {
        System.out.println("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! 即使text2ListOfLists 包含:key12 key13 key14 key15 key16key13-key17 -&gt; **MATCH!!** If any entry in List: textList1` 作为条目出现在任何子列表中,它也会产生NO MATCH!! > of text2ListOfLists = true TX
    • @S.O.S 是您原始代码的输出还是我建议的代码的输出?如果是我的代码输出,请用所有列表的完整声明和初始化编辑您的问题,以便我自己检查。
    • 使用您的代码 + 输出更新了 OP。如前所述,我期待 MATCH!而不是 NO MATCH!!
    • @S.O.S 我不知道StringX.splitStrIntoWordChunks() 是什么,所以我无法测试它。你能用这些调用返回的实际列表来替换这些调用吗?
    • StringX.splitStrIntoWordChunks() 是我在 SO 上发布的以下代码的精确副本:stackoverflow.com/a/60033687/8814573 如果您希望我发布更多代码,请告诉我。谢谢!
    【解决方案2】:

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

    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;
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 2021-04-14
      • 2019-04-04
      • 2014-07-31
      • 2020-08-18
      • 2012-11-09
      相关资源
      最近更新 更多