简体   繁体   English

检查列表是否<string>包含来自另一个列表的文本<string></string></string>

[英]Checking to see if a list<string> contains text from another list<string>

I have two different list.我有两个不同的列表。 One contains only service titles, and the other contains long sentences with the service titles in the text.一个只包含服务标题,另一个包含带有文本中服务标题的长句。 I'm interested in finding a way to determining if the titles from one list are present within the long sentences of the other list.我有兴趣找到一种方法来确定一个列表中的标题是否存在于另一个列表的长句子中。 I'm not looking for whether these two list are an exact match.我不是在寻找这两个列表是否完全匹配。 Only if one of them contains the contents of the other.仅当其中一个包含另一个的内容时。

[Ex] Want to verify that the string "Application Scan" is present within the text in "This is an example sentence that has Application Scan in it" in list2 [Ex] 想要验证字符串“Application Scan”是否存在于 list2 中“This is an example sentence that has Application Scan in it”的文本中

List list1 = new ArrayList();列表 list1 = new ArrayList(); List list2 = new ArrayList();列表 list2 = new ArrayList();

    list1.add("Application Scan"); 
    list1.add("Antivirus and HIPS"); 
    list1.add("Backup Administration"); 

    list2.add("This is an example sentence that has Application Scan in it"); 
    list2.add("This is an example sentence that has Antivirus and HIPS in it"); 
    list2.add("This is an example sentence that has Backup Administration in it"); 


if (list2.containsAll(list1)) {
        System.out.print("Yes, list1 contents are in list2");
    }
    else {
        System.out.println("No, list1 contents are not within list2");
    }

My approach so far keeps saying that the list1 contents are not found within list2 and I'm not sure how to fix this exactly到目前为止,我的方法一直说在 list2 中找不到 list1 内容,我不确定如何准确解决这个问题

If you are using Java 8 you can use:如果您使用的是 Java 8,您可以使用:

if (list2.stream().anyMatch(l -> list1.stream().anyMatch(l::contains))) {

Which mean, to check each element in list2 can contain any element in list1这意味着,要检查 list2 中的每个元素可以包含 list1 中的任何元素

You will have to iterate both lists.您将不得不迭代这两个列表。 Since the list2 contains the big texts, the outer loop will be for list2:由于 list2 包含大文本,因此外部循环将用于 list2:

public static void main(String[] args) {
    List<String> list1 = new ArrayList<>();
    list1.add("Application Scan");
    list1.add("Antivirus and HIPS");
    list1.add("Backup Administration");

    List<String> list2 = new ArrayList<>();
    list2.add("This is an example sentence that has Application Scan in it");
    list2.add("This is an example sentence that has Antivirus and HIPS in it");
    list2.add("This is an example sentence that has Backup Administration in it");

    list2.forEach(bigtext -> {
        list1.forEach(service -> {
            if (bigtext.contains(service)) {
                System.out.println(service);
            }
        });
    });
}

prints:印刷:

Application Scan
Antivirus and HIPS
Backup Administration

It is the same with doing this:这样做是一样的:

for (String bigText : list2) {
    for (String service : list1) {
        if (bigText.contains(service))
            System.out.println(service);
    }
}

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

相关问题 将字符串与数组列表进行比较,以查看字符串是否包含列表中的任何内容 - Comparing a string with an array list to see if the string contains anything from the list 从列表中查找字符串并查看是否部分在另一个列表中 - Find string from a list and see if partially in an another list 检查列表中的字符串是否两次包含来自不同列表的部分字符串,并在处理列表后返回唯一列表 - Checking if the string within the list contains portion of the string from different list twice, and return unique list after processing the list 使用 Streams,查看列表是否包含来自另一个列表的 object 的属性 - Using Streams, see if a list contains a property of an object from another list 拆分文本包含数字到字符串列表 - Split text contains number to list of string 检查一个String数组是否包含另一个String数组的所有值(Java) - Checking to see if a String array contains all of the values of another String array (Java) 检查字符串是否包含另一个完整的字符串 - Checking if String contains another whole String 字符串包含与列表包含 - String contains versus List contains 名单 <Enum> 包含String - List<Enum> contains String 如何从Firebase中检索包含另一个字符串列表的完整对象列表? - How to retrieve full List Of Objects from Firebase that contains another List of String in it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM