Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java8中,从一个ArrayList中查找一个字符串作为另一个ArrayList字符串的字符序列_Java_Arraylist - Fatal编程技术网

在Java8中,从一个ArrayList中查找一个字符串作为另一个ArrayList字符串的字符序列

在Java8中,从一个ArrayList中查找一个字符串作为另一个ArrayList字符串的字符序列,java,arraylist,Java,Arraylist,我的Java例程应该从一个ArrayList中找到一个字符串作为另一个ArrayList字符串的字符序列 如果我从目标ArrayList中的源ArrayList中搜索字符串项,它会工作。 如果目标ArrayList项有更多字母数字文字,我会尝试使用字符序列查找所需的部分 简短的文字示例: 阵列列表A项目:A0B、C1D、E2F 阵列列表B项:A0B 结果:在ArrayList_A Items中找到项目B有效 阵列列表A项目:A0B/C1D、E2F 阵列列表B项:A0B 结果:在ArrayList

我的Java例程应该从一个ArrayList中找到一个字符串作为另一个ArrayList字符串的字符序列

如果我从目标ArrayList中的源ArrayList中搜索字符串项,它会工作。 如果目标ArrayList项有更多字母数字文字,我会尝试使用字符序列查找所需的部分

简短的文字示例:

阵列列表A项目:A0B、C1D、E2F 阵列列表B项:A0B

结果:在ArrayList_A Items中找到项目B有效

阵列列表A项目:A0B/C1D、E2F 阵列列表B项:A0B

结果:在ArrayList_A Items中找到项目B这是我另外想要的

下面是我的部分工作代码。有人能帮我修一下吗?这样我就可以从解决方案中学习了

import java.util.ArrayList;

public class Checking {

    static void checkValues () {

        ArrayList<String> arrayListA = new ArrayList();
        arrayListA.add("A0B/C1D");
        arrayListA.add("E2F");
        System.out.println("\narrayListA: " + arrayListA);
        int sizeLA = arrayListA.size();
        System.out.println("arrayListA Length:" + sizeLA);

        ArrayList<String> arrayListB = new ArrayList();
        arrayListB.add("A0B");
        arrayListB.add("E2F");
        System.out.println("\narrayListB: " + arrayListB);
        int sizeLB = arrayListB.size();
        System.out.println("arrayListB Length:" + sizeLB);

        ArrayList<String> arrayListFound = new ArrayList();

        //Something must be fixed here...
        arrayListB.stream().forEach((searchStr) -> {
            System.out.print("\nstr Search Item is: " + searchStr);
            if(arrayListA.contains((CharSequence)searchStr)){
                System.out.print(" - found!" );
                arrayListFound.add((String) searchStr);
            }
            else {
                System.out.print(" - not found");
            }
        });

        System.out.println("\n\narrayListFound Items: " + arrayListFound);
        int sizeListFound = arrayListFound.size();  
        System.out.println("sizeListFound Length :" + sizeListFound);   
        System.out.println("Expected to be fully found in arrayListFound: " + arrayListB); 
    }   

    public static void main(String[] args) {
        checkValues();
    }   
}

您可以对这两个集合进行流式处理,并使用Stringcontains而不是Listcontains

它会将A中包含的B中的所有对应字符串对象添加到arrayListFound


您可以对这两个集合进行流式处理,并使用Stringcontains而不是Listcontains

它会将A中包含的B中的所有对应字符串对象添加到arrayListFound

这段代码说,如果我的列表正好包含这个字符串,那么它就被找到了。但是字符串A0B与A0B/C1D不同。在这两个列表上迭代并检查contains方法String而不是ArrayList,您将找到所需的内容。例如:

arrayListB.stream().forEach((searchStr) -> {
    System.out.print("\nstr Search Item is: " + searchStr);
    arrayListA.stream().forEach((sndStr) -> {
        if (sndStr.contains(searchStr) || searchStr.contains(sndStr)) {
            System.out.print(" - found!");
            arrayListFound.add((String) searchStr);
        } else {
            System.out.print(" - not found");
        }
    });
});
这段代码说,如果我的列表正好包含这个字符串,那么它就被找到了。但是字符串A0B与A0B/C1D不同。在这两个列表上迭代并检查contains方法String而不是ArrayList,您将找到所需的内容。例如:

arrayListB.stream().forEach((searchStr) -> {
    System.out.print("\nstr Search Item is: " + searchStr);
    arrayListA.stream().forEach((sndStr) -> {
        if (sndStr.contains(searchStr) || searchStr.contains(sndStr)) {
            System.out.print(" - found!");
            arrayListFound.add((String) searchStr);
        } else {
            System.out.print(" - not found");
        }
    });
});

下面是一个仅稍微修改您的实现的替代解决方案

arrayListB.stream().forEach((searchStr) -> {
    if(arrayListA.stream().anyMatch(str -> str.contains((CharSequence)searchStr))){
        System.out.print(" - found!" );
        arrayListFound.add((String) searchStr);
    }
    else {
        System.out.print(" - not found");
    }
});
一,。我们可以使用任何比foreach更好的匹配

二,。这里我们检查arrayListA中是否存在任何将arrayListB中的searchStr作为子字符串的字符串

为了在不改变arrayListFound列表的情况下以纯粹的功能性风格进行操作,可以采用以下方法:

Predicate<String> isInArrayA = searchStr-> arrayListA.stream().anyMatch(str -> str.contains(searchStr));
arrayListFound = (ArrayList<String>) arrayListB.stream()
                .filter(isInArrayA)
                .collect(Collectors.toList());

下面是一个仅稍微修改您的实现的替代解决方案

arrayListB.stream().forEach((searchStr) -> {
    if(arrayListA.stream().anyMatch(str -> str.contains((CharSequence)searchStr))){
        System.out.print(" - found!" );
        arrayListFound.add((String) searchStr);
    }
    else {
        System.out.print(" - not found");
    }
});
一,。我们可以使用任何比foreach更好的匹配

二,。这里我们检查arrayListA中是否存在任何将arrayListB中的searchStr作为子字符串的字符串

为了在不改变arrayListFound列表的情况下以纯粹的功能性风格进行操作,可以采用以下方法:

Predicate<String> isInArrayA = searchStr-> arrayListA.stream().anyMatch(str -> str.contains(searchStr));
arrayListFound = (ArrayList<String>) arrayListB.stream()
                .filter(isInArrayA)
                .collect(Collectors.toList());

首先,您应该使用函数式编程。forEach不允许您超过标准的

精确匹配

ArrayList<String> arrayListFound = arrayListB.stream()
          .filter(arrayListA::contains)
          .collect(Collectors.toList());
子串匹配

ArrayList<String> arrayListFound = arrayListB.stream()
          .filter(itemB -> arrayListA.stream()
                 .matchAny(itemA -> itemA.contains(itemB))
           )
          .collect(Collectors.toList());

首先,您应该使用函数式编程。forEach不允许您超过标准的

精确匹配

ArrayList<String> arrayListFound = arrayListB.stream()
          .filter(arrayListA::contains)
          .collect(Collectors.toList());
子串匹配

ArrayList<String> arrayListFound = arrayListB.stream()
          .filter(itemB -> arrayListA.stream()
                 .matchAny(itemA -> itemA.contains(itemB))
           )
          .collect(Collectors.toList());

我想加上这个,但因为你做了+1,我想加上这个,但因为你做了+1,我非常感谢所有做出贡献的人。现在我明白了,这是可行的。非常感谢所有做出贡献的人。现在我明白了,这是可行的。