Java 8流过滤:IN子句 [英] Java 8 stream filtering: IN clause
本文介绍了Java 8流过滤:IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List<Y> tmp= new DATA<Y>().findEntities();
List<X> tmp1 = new DATA<X>().findEntities().stream().filter(
IN (tmp) ???
).collect(Collectors.toList());
如何使用谓词模拟典型的IN子句(如mysql或JPA)?
How to simulate a tipical IN clause (like in mysql or JPA) using a Predicate ?
推荐答案
我决定将评论更新为答案.您请求的Predicate<Y>
(其中Y应该是具体类型)的lambda表达式如下:
I decided to update my comment to an answer. The lambda expression for your requested Predicate<Y>
(where Y should be a concrete type) looks as following:
element -> tmp.contains(element)
由于集合的contains
方法与谓词的test
方法具有相同的签名,因此可以使用方法引用(此处为实例方法引用):
Because the collection's contains
method has the same signature as the predicate's test
method, you can use a method reference (here an instance method reference):
tmp::contains
完整示例:
List<Number> tmp = Arrays.asList(1, 2, 3);
List<Integer> tmp1 = Arrays
.stream(new Integer[] { 1, 2, 3, 4, 5 })
.filter(tmp::contains)
.collect(Collectors.toList());
System.out.println(tmp1);
此打印
[1, 2, 3]
这篇关于Java 8流过滤:IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文