Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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
使用与另一个列表Java流的全部内容匹配的条目筛选列表的内容_Java_Java Stream - Fatal编程技术网

使用与另一个列表Java流的全部内容匹配的条目筛选列表的内容

使用与另一个列表Java流的全部内容匹配的条目筛选列表的内容,java,java-stream,Java,Java Stream,我可能无法正确搜索,但这是一个理论上很简单的查询。我有一个包含数字的数组列表。我想筛选与另一个列表中的所有条目相匹配的列表 下面是我的清单 {1,2,3},{1,2},{1},{2},{3},{2,3} 现在,如果我的条件列表是{1,2},我应该得到以下结果 {1,2,3},{1,2} 基本上我在寻找所有符合条件的列表。我尝试在谓词中使用contains,但其返回值类似于或条件 //internally it creates array products.add(new ProdData(34

我可能无法正确搜索,但这是一个理论上很简单的查询。我有一个包含数字的数组列表。我想筛选与另一个列表中的所有条目相匹配的列表

下面是我的清单 {1,2,3},{1,2},{1},{2},{3},{2,3}

现在,如果我的条件列表是{1,2},我应该得到以下结果 {1,2,3},{1,2}

基本上我在寻找所有符合条件的列表。我尝试在谓词中使用contains,但其返回值类似于或条件

//internally it creates array 
products.add(new ProdData(344, 766));
products.add(new ProdData(344,123));
products.add(new ProdData(344,766,123));

List<Integer> matchingVolumes = new ArrayList<>();
    matchingVolumes.add(344);
    matchingVolumes.add(766);
    
products.stream()
    .map(ProdData::getChemVolume)
    .filter(p -> {return matchingVolumes.contains(p);})
    .collect(Collectors.toList());
//它在内部创建数组
新增(新产品数据(344766));
新增(新产品数据(344123));
新增(新产品数据(344766123));
List matchingVolumes=new ArrayList();
匹配卷。添加(344);
匹配卷。添加(766);
products.stream()
.map(ProdData::getChemVolume)
.filter(p->{return matchingVolumes.contains(p);})
.collect(Collectors.toList());
我想匹配记录#1和3

products是ProdData对象的列表,chemVolume是我试图匹配标准的字段。条目的顺序并不重要。

您可以创建列表,并尝试迭代主列表,检查筛选列表中的所有元素是否与主列表的子列表匹配

class Sample {
    public static void main(String[] args) {
        List<List<Integer>> mainList = List.of(List.of(1, 2, 3), List.of(1, 2), List.of(2));
        List<Integer> filter = List.of(1, 2);
        System.out.println("  " + mainList.stream().filter(integerList -> isListContains(filter, integerList)).collect(Collectors.toList()));
    }

    public static boolean isListContains(List<Integer> filter, List<Integer> mainList) {
        return filter.stream()
                .allMatch(
                        mainList.stream()
                                .collect(Collectors.toSet())
                                ::contains);
    }

}
类示例{
公共静态void main(字符串[]args){
List mainList=List.of(List.of(1,2,3),List.of(1,2),List.of(2));
列表过滤器=列表(1,2);
System.out.println(“+mainList.stream().filter(integerList->isListContains(filter,integerList)).collect(collector.toList());
}
公共静态布尔isListContains(列表筛选器、列表mainList){
返回filter.stream()
.所有比赛(
mainList.stream()
.collect(收集器.toSet())
::包含);
}
}
您可以通过使用如上所述的流和过滤条件来实现相同的功能。
对于筛选,您可以使用filter.stream()allMatch(您可以从这里了解更多信息。

您似乎在寻找的是:

.filter(p -> p.getAllChemVolumes().containsAll(matchingVolumes))

其中,您需要在
ProductData
中定义
getAllChemVolumes
方法,以根据您寻求的效率返回
集合。

如果您的意思是希望代码每次将您的标准列表与另一个列表中的参数进行比较(如果不匹配,则打印它)。 我建议您使用流(对于Java8)

静态布尔x=false;
公共静态void main(字符串[]args){
List list1=新的ArrayList(Arrays.asList(1,2,3),Arrays.asList(1),Arrays.asList(1,2),Arrays.asList(2),Arrays.asList(3),Arrays.asList(3,2));
List list2=新的ArrayList(Arrays.asList(1,2));
消费者检查=list->{if(list.equals(list2))x=true;};
消费者打印列表=ListPrinted->{if(x==false){System.out.print(ListPrinted);};
list1.stream().peek(检查).forEach(打印列表);
}
它有效,我编译了它

你可能会问,为什么布尔X?!你可以把它看作是停止打字的条件!! 为什么我将其设置为静态和全局变量??: 因为在lambda表达式中,所有局部变量都必须是final(我们不希望如此!),并且我们在一个类下工作(我们没有几个对象可以使用),所以我使用static(变量是静态的:它是类变量)


我希望你能理解。

我想你想要这样的东西。如果你的底层数组是对象,这是最简单的,这样你就可以用它们创建列表。如果它们是原语,你可能想在过滤器中使用arrays.binarySearch,但是你必须在过滤器之前在流中进行arrays.sort:

Integer[][] values = {{1,5,3},{1,3},{5,1},{3,5}};
Integer[] filter = {3,5};

Arrays.stream(values)
      .map (x -> List.of(x))
      .filter( x -> Arrays.stream(filter)
                          .allMatch(y -> x.indexOf(y)>=0))
      .forEach(System.out::println);

// output:
// [1, 5, 3]
// [3, 5]

你能发布你的代码吗?数组是字面上的数组吗,例如
int[]
或者它们是列表?发布代码以便我们可以测试、尝试并使用locally条目的顺序是否重要,或者如果条目存在于数组中的某个位置是否足够?@niteen22请在问题中编辑其他信息,您可以很好地格式化代码。您可以编辑问题以包含创建问题的代码吗“包含数字的数组列表”?
Integer[][] values = {{1,5,3},{1,3},{5,1},{3,5}};
Integer[] filter = {3,5};

Arrays.stream(values)
      .map (x -> List.of(x))
      .filter( x -> Arrays.stream(filter)
                          .allMatch(y -> x.indexOf(y)>=0))
      .forEach(System.out::println);

// output:
// [1, 5, 3]
// [3, 5]