如何使用java从两个数组中获取不常见的元素

时间:2018-03-24 07:59:09

标签: java

我有两个字符串数组

array1={"Hardware Docs","Customer Data","Customer Docs","Commit Reviews","Product Planning Docs","Business Operations ","Proprietary"};
array2={"Hardware Docs","Customer Docs"};

我想要来自这两个数组的不常见字符串。任何人都建议如何操作。

1 个答案:

答案 0 :(得分:-1)

    String [] array1={"Hardware Docs","Customer Data","Customer Docs","Commit Reviews","Product Planning Docs","Business Operations ","Proprietary"};
    String[] array2={"Hardware Docs","Customer Docs"};

    ArrayList<String> filter1 = new ArrayList<>();

    for(String str1: array1){
        boolean match = false;
        for(String str2: array2){
            if(str1.equals(str2))
                match = true;
        }
        if(!match)
            filter1.add(str1);
    }

但这是蛮力。如果性能对您很重要,您应该寻找更好的解决方案