这个需求是在批量入库的时候,进行批量校验用到的
1.逻辑是传入List参数,需要入库
2.根据List去查询库中是否存在
3.用传入的List进行过滤,获取苦衷不存在的resList
4.插入resList(这样可以保证入库不重复)
实体类User
package test.send; public class User { private String id; private String name; private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }View Code
有两个List
list1要过滤到list2中已存在的(过滤判断条件是name相同)
List<User> list1 = new ArrayList<>(); User u1 = new User(); u1.setName("u1"); u1.setAge(12); User u2 = new User(); u2.setName("u2"); u2.setAge(13); User u3 = new User(); u3.setName("u3"); u3.setAge(13); list1.add(u1); list1.add(u2); list1.add(u3);
List<User> list2 = new ArrayList<>(); User u3 = new User(); u3.setName("u3"); u3.setAge(13); User u4 = new User(); u3.setName("u4"); u3.setAge(13);
list1.add(u3);
list1.add(u4);
//resList 过滤后的结果 List<User> resList = list1 .stream().filter(new Predicate<User>() { @Override public boolean test(User u) { //如根据name过滤 for (User user : list2) { if(u.getName().equals(user.getName())){ return false; } } return true; } }).collect(Collectors.toList()); //resList 输出为 u1 u2
标签:Java,List,list1,u3,User,泛型,new,public From: https://www.cnblogs.com/JYB2021/p/16770277.html