我可能无法正确搜索,但这是一个理论上很简单的查询。我有包含数字的数组列表。我想过滤与另一个列表中的所有条目匹配的列表。
说以下是我的 list {1,2,3},{1,2},{1},{2},{3},{2,3}
现在如果我的条件列表是 {1,2} 我应该得到以下结果 {1,2,3},{1,2}
基本上我正在寻找标准列表的所有匹配项。我尝试在谓词中使用 contains 但它的返回类似于 or 条件
//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());
我想匹配记录 # 1 和 3
products 是 ProdData 对象的列表,其中 chemVolume 是我尝试匹配条件的字段。条目顺序并不重要。
最佳答案
如果您的意思是您需要一个代码,每次将您的条件列表与其他列表中的参数进行比较(如果不匹配则打印它)。 我建议你使用 Streams(for Java 8)
static Boolean x = false ;
public static void main(String[] args) {
List<List<Integer>> list1 = new ArrayList<>(Arrays.asList(Arrays.asList(1,2,3),Arrays.asList(1),Arrays.asList(1,2),Arrays.asList(2),Arrays.asList(3),Arrays.asList(3,2)));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,2));
Consumer<List<Integer>> check = list->{if(list.equals(list2)) x = true ;};
Consumer<List<Integer>> printList = ListPrinted ->{if(x==false){ System.out.print(ListPrinted); }} ;
list1.stream().peek(check).forEach(printList);
}
它有效,我编译了它
你可能会问,为什么是 boolean 值 x !!?您可以将其视为停止打字的条件! 为什么我将其设为静态和全局变量? : 因为在 lambda 表达式中,所有局部变量都必须是 Final(我们不希望这样!),并且我们在一个类下工作(我们没有多个对象可以使用),所以我使用 static(变量是static:它是一个类变量)。
希望你能理解。
关于java - 使用与另一个列表的整个内容匹配的条目过滤列表的内容 Java 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63634495/