rt,比如有个ArrayList a,泛型为B,我想去判断如果B中某个字段(假如getFieldX)不为null,执行一种操作,为null执行另一种,请问框架怎么写?要函数式那种
函数式?你指的是lambda表达式?
List<Person> list = new ArrayList<>();
list.addAll(Arrays.asList(new Person("a"), new Person("b"), new Person(), new Person("c"), new Person()));
list = list.stream()//创建stream
.map((p) -> {
if (p.getName() == null) {
//为空时执行的操作
p.setName("hello");
} else {
//不为空要执行的操作
p.setName(null);
}
return p;
})//转换stream,返回值仍为stream。所有转换strem操作为惰性,直到调用汇聚函数才一并执行,
.collect(Collectors.toList());//汇聚函数,计算结果返回为List类型
System.out.println(list);