我必须通过Map过滤对象集合,它保存对象字段名称和字段值的键值对.我试图通过stream().filter()应用所有过滤器.
对象实际上是JSON,因此Map保存其变量的名称以及它们必须包含的值以便被接受,但为了简单起见并且因为它与问题无关,我编写了一个简单的Testclass来模拟行为:
public class TestObject {
private int property1;
private int property2;
private int property3;
public TestObject(int property1, int property2, int property3) {
this.property1 = property1;
this.property2 = property2;
this.property3 = property3;
}
public int getProperty(int key) {
switch(key) {
case 1: return property1;
case 2: return property2;
default: return property3;
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试了什么:
public static void main(String[] args) {
List<TestObject> list = new ArrayList<>();
Map<Integer, Integer> filterMap = new HashMap<>();
list.add(new TestObject(1, 2, 3));
list.add(new TestObject(1, 2, 4));
list.add(new TestObject(1, 4, 3));
filterMap.put(3, 3); //Filter property3 == 3
filterMap.put(2, 2); //Filter property2 == 2
//Does not apply the result
filterMap.forEach((key, value) -> list.stream()
.filter(testObject -> testObject.getProperty(key) == value)
.collect(Collectors.toList())
);
/* Gives error: boolean can not be converted to void
list = list.stream()
.filter(testObject -> filterMap.forEach((key, value) -> testObject.getProperty(key) == value))
.collect(Collectors.toList()
);
*/
//Printing result
list.forEach(obj -> System.out.println(obj.getProperty(1) + " " + obj.getProperty(2) + " " + obj.getProperty(3)));
}
Run Code Online (Sandbox Code Playgroud)
我首先尝试将forEach of the Map和Collection的流首先放在一起,但两种解决方案都没有按预期工作.此示例的所需输出仅用于打印具有值property1 = 1,property2 = 2和property3 = 3的对象.
如何正确应用所有过滤器,就像在具有固定数量的过滤器的代码中一个接一个地放置它们一样?
使用已知数量的过滤器:
list.stream().filter(...).filter(...)
Run Code Online (Sandbox Code Playgroud)
编辑:
Sweeper在他的回答中总结了我的问题,所以这里只是为了澄清(可能是未来的读者):我想保留满足所有过滤器的所有对象.
Swe*_*per 14
我想你想保持TestObjects满足地图指定的所有条件的所有条件?
这将完成工作:
List<TestObject> newList = list.stream()
.filter(x ->
filterMap.entrySet().stream()
.allMatch(y ->
x.getProperty(y.getKey()) == y.getValue()
)
)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
翻译成"英文",
filterlist通过保留以下所有元素的列表x:
- 所有的键值对
y的filterMap必须满足:
x.getProperty(y.getKey()) == y.getValue()
(我认为我没有把这个人类可读的做得很好......)如果你想要一个更易读的解决方案,我推荐Jeroen Steenbeeke的回答.
要将可变数量的过滤器步骤应用于流(仅在运行时已知),您可以使用循环来添加过滤器步骤.
Stream<TestObject> stream = list.stream();
for (Predicate<TestObject> predicate: allPredicates) {
stream = stream.filter(predicate);
}
list = stream.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4833 次 |
| 最近记录: |