1、使用filter和allMatch实现
filter
对列表中的每个对象应用过滤条件,保留满足条件的对象。allMatch
对 filterMap
中的每个条件 (Map.Entry<Integer, Integer>
),检查 TestObject
是否满足所有条件。利用 filterMap
提供了灵活性,可以轻松修改过滤条件,而无需更改代码。
import java.util.*; import java.util.stream.Collectors; public class TestObjectExample { public static void main(String[] args) { // 创建 TestObject 列表 List<TestObject> list = Arrays.asList( new TestObject(10, 20, 30), new TestObject(15, 25, 35), new TestObject(10, 25, 40) ); // 创建 filterMap,定义过滤条件 Map<Integer, Integer> filterMap = new HashMap<>(); filterMap.put(1, 10); // property1 必须等于 10 filterMap.put(2, 25); // property2 必须等于 25 // 使用 Stream 进行过滤 List<TestObject> newList = list.stream() .filter(x -> filterMap.entrySet().stream() .allMatch(y -> x.getProperty(y.getKey()) == y.getValue()) ) .collect(Collectors.toList()); // 输出结果 System.out.println("过滤后的列表:"); for (TestObject obj : newList) { System.out.println("property1: " + obj.getProperty(1) + ", property2: " + obj.getProperty(2) + ", property3: " + obj.getProperty(3)); } } } // TestObject 类 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; } } }
2、使用for实现多个过滤条件
allPredicates
是一个 Predicate<TestObject>
的列表,存储了所有的过滤条件。通过 for
循环,逐步将每个过滤条件应用到流中。使用 collect(Collectors.toList())
将过滤后的结果收集为列表。可以随时向 allPredicates
添加更多条件,无需修改核心逻辑。
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class TestObjectExample { public static void main(String[] args) { // 初始化数据 List<TestObject> list = Arrays.asList( new TestObject(10, 20, 30), new TestObject(15, 25, 35), new TestObject(20, 30, 40), new TestObject(25, 35, 45) ); // 定义多个过滤条件 List<Predicate<TestObject>> allPredicates = new ArrayList<>(); allPredicates.add(obj -> obj.getProperty(1) > 15); // property1 > 15 allPredicates.add(obj -> obj.getProperty(2) < 36); // property2 < 35 allPredicates.add(obj -> obj.getProperty(3) != 40); // property3 != 40 // 使用for循环应用过滤条件 Stream<TestObject> stream = list.stream(); for (Predicate<TestObject> predicate : allPredicates) { stream = stream.filter(predicate); } // 收集过滤结果 list = stream.collect(Collectors.toList()); // 输出结果 System.out.println("过滤后的列表:"); for (TestObject obj : list) { System.out.println("property1: " + obj.getProperty(1) + ", property2: " + obj.getProperty(2) + ", property3: " + obj.getProperty(3)); } } } // TestObject 类 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; } } }
3、使用foreach和filter实现
filterMap
定义了过滤条件,其中 key
表示要检查的属性编号,value
是该属性需要满足的值。对于每个条件,过滤 list
中的 TestObject
,生成符合条件的子列表。filter
调用 testObject.getProperty(key)
动态获取指定属性值,并与条件值 value
比较。collect(Collectors.toList())
将过滤结果收集为一个新的列表。
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // 创建TestObject列表 List<TestObject> list = Arrays.asList( new TestObject(1, 2, 3), new TestObject(4, 5, 6), new TestObject(7, 8, 9), new TestObject(1, 5, 3) ); // 创建过滤条件的Map Map<Integer, Integer> filterMap = new HashMap<>(); filterMap.put(1, 1); // 过滤条件: property1 == 1 filterMap.put(2, 5); // 过滤条件: property2 == 5 // 使用forEach和filter进行筛选 filterMap.forEach((key, value) -> { List<TestObject> filteredList = list.stream() .filter(testObject -> testObject.getProperty(key) == value) .collect(Collectors.toList()); // 输出每个key对应的过滤结果 System.out.println("Key: " + key + ", Value: " + value + ", Filtered List: " + filteredList); }); } } // TestObject类 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; } } @Override public String toString() { return "TestObject{" + "property1=" + property1 + ", property2=" + property2 + ", property3=" + property3 + '}'; } }