java怎么筛选list中相同的值和筛选不同的值
这篇文章主要介绍了java怎么筛选list中相同的值和筛选不同的值的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java怎么筛选list中相同的值和筛选不同的值文章都会有所收获,下面我们一起来看看吧。
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、成都小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了洞头免费建站欢迎大家使用!
代码如下:
public class People { private String id; private String somethingElse; public People() { } public People(String id, String somethingElse) { this.id = id; this.somethingElse = somethingElse; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSomethingElse() { return somethingElse; } public void setSomethingElse(String somethingElse) { this.somethingElse = somethingElse; } @Override public String toString() { return "People{" + "id='" + id + '\'' + ", somethingElse='" + somethingElse + '\'' + '}'; } }
people实体类和student实体类
People类
student public class Student { private String id; private String idCard; private String somethingElse; public Student() { } public Student(String id, String idCard, String somethingElse) { this.id = id; this.idCard = idCard; this.somethingElse = somethingElse; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getSomethingElse() { return somethingElse; } public void setSomethingElse(String somethingElse) { this.somethingElse = somethingElse; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", idCard='" + idCard + '\'' + ", somethingElse='" + somethingElse + '\'' + '}'; } }
测试类
public static void main(String[] args) throws Exception{ //初始化数据 ListstudentList = new ArrayList(){{ add(new Student("1", "11", "111")) ; add(new Student("2", "22", "222")); add(new Student("3", "33", "333")); }}; List peopleList = new ArrayList(){ { add(new People("11", "111")); add(new People("222", "222")); add(new People("33", "333")); }}; //获取相同字段内容,转化为set Set ids = peopleList .stream() .map(People::getId) .collect(Collectors.toSet());//过滤重复内容 List result = studentList .stream() .filter(e -> ids.contains(e.getIdCard())) .collect(Collectors.toList()); System.out.println(result); //获取相同字段内容,转化为set Set ids = peopleList .stream() .map(People::getId) .collect(Collectors.toSet());//过滤重复内容 List result = studentList .stream() .filter(e -> !ids.contains(e.getIdCard())) .collect(Collectors.toList()); System.out.println(result); }
关于“java怎么筛选list中相同的值和筛选不同的值”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“java怎么筛选list中相同的值和筛选不同的值”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。
网页名称:java怎么筛选list中相同的值和筛选不同的值
URL分享:http://lswzjz.com/article/jjpodh.html