标签:class lag static optional ide import port flag return
目标:测试option的过滤,链式操作:
代码:
package test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.function.Consumer; import java.util.function.Predicate; public class optionnaltest { class user{ String name; String age; user(){} user(String name,String age){this.name = name;this.age=age;} void intoduce() { System.out.println("i am "+name+",i am "+age+" this year!"); } String tosString() { return new StringBuffer().append("{name:").append(name+",age:").append(age+"}").toString(); } } public static void main(String[] args) { List<user> list = new ArrayList<user>(); Arrays.asList(10,11,12,14,16,20).forEach(new Consumer<Integer>() { @Override public void accept(Integer t) { optionnaltest op = new optionnaltest(); if(t <12) list.add(op.new user()); else list.add(op.new user("name"+t,t+"")); } }); List<user> tm = new ArrayList<user>(); list.forEach(new Consumer<user>() { @Override public void accept(user t) { boolean flag = Optional.ofNullable(t).filter(s->s.name!=null).isPresent(); tm.add(!flag?t:null); } }); list.removeAll(tm); list.forEach(new Consumer<user>() { @Override public void accept(user t) { System.out.println(t.tosString()); } }); } }
标签:class lag static optional ide import port flag return
原文地址:https://www.cnblogs.com/g177w/p/10795499.html