1.定义一个实体体
public class Customer {
private String name;
private int points;
//Constructor and standard getters
}
2.创建一个实体类集合
Customer john = new Customer("John P.", 15);
Customer sarah = new Customer("Sarah M.", 200);
Customer charles = new Customer("Charles B.", 150);
Customer mary = new Customer("Mary T.", 1);
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
3. 数据筛选
List<Customer> customersWithMoreThan100Points = customers
.stream()
.filter(c -> c.getPoints() > 100)
.collect(Collectors.toList());
附录
Arrays.asList 源码
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
Stream collect 转换stream对象为指定的类型
public static <T>
Collector<T, ?, List<T>> toList() {
return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
(left, right) -> { left.addAll(right); return left; },
CH_ID);
}