获取两个List中重复或不同的元素---removeAll()和addAll()的使用
2015-07-25 23:19
579 查看
昨天项目中遇到了一个将查询结果增量发出的,即每次比上次增的获取。
这个问题同事去网上找了好多高人写的代码,我看了好久,感觉被绕了,最后想着还是用最简单的方式来做,可能好一些。
这个问题同事去网上找了好多高人写的代码,我看了好久,感觉被绕了,最后想着还是用最简单的方式来做,可能好一些。
package com.paic.dto; public class Student { private Integer id; private String name; private String sex; private String other; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((other == null) ? 0 : other.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (this.other == null) { if (other.other != null) return false; } else if (!this.other.equals(other.other)) return false; return true; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", other=" + other + "]"; } }
package com.paic.first_maven; import java.util.ArrayList; import java.util.List; import com.paic.dto.Student; /** * Hello world! * */ public class App { public static void main( String[] args ) { // System.out.println( "Hello World!" ); // List<Integer> list = new ArrayList<Integer>(); // List<Integer> list1 = new ArrayList<Integer>(); // list.add(1); // list.add(2); // list.add(3); // list.add(4); // list1.addAll(list); // list1.add(5); //// list1.removeAll(list);//list1=[5] list=[1,2,3,4] // list.removeAll(list1);//list=[] list1=[1,2,3,4,5] // System.out.println(list1+"\n"+list); List<Student> stu1 = new ArrayList<Student>(); List<Student> stu2 = new ArrayList<Student>(); Student s0 = new Student(); s0.setId(0); s0.setName("张三0"); s0.setOther("aa0"); s0.setSex("男0"); stu1.add(s0); Student s1 = new Student(); s1.setId(1); s1.setName("张三1"); s1.setOther("aa1"); s1.setSex("男1"); stu1.add(s1); Student s2 = new Student(); s2.setId(2); s2.setName("张三2"); s2.setOther("aa2"); s2.setSex("男2"); stu1.add(s2); Student s3 = new Student(); s3.setId(3); s3.setName("张三3"); s3.setOther("aa3"); s3.setSex("男3"); stu1.add(s3); Student s31 = new Student(); s31.setId(3); s31.setName("张三3"); s31.setOther("aa31"); s31.setSex("男3"); stu2.add(s1); stu2.add(s2); stu2.add(s31); Student s4 = new Student(); s4.setId(4); s4.setName("张三4"); s4.setOther("aa4"); s4.setSex("男4"); stu2.add(s4); stu2.removeAll(stu1);//list1=[5] list=[1,2,3,4] // list.removeAll(list1);//list=[] list1=[1,2,3,4,5] System.out.println(stu1+"\n"+stu2); //输出结果如下: // stu1=[Student [id=0, name=张三0, sex=男0, other=aa0], Student [id=1, name=张三1, sex=男1, other=aa1], Student [id=2, name=张三2, sex=男2, other=aa2], Student [id=3, name=张三3, sex=男3, other=aa3]] // stu2=[Student [id=3, name=张三3, sex=男3, other=aa31], Student [id=4, name=张三4, sex=男4, other=aa4]] } }
相关文章推荐
- oracle复制表及备份数据
- HTML5 File API 简介
- [cogs 908]校园网
- 选择Redis作为分布式缓存
- NOIP 2014 飞扬的小鸟
- linux系统编程:线程同步-读写锁(rwlock)
- 2009 Multi-University Training Contest 10 - Host by NIT
- 线段覆盖3
- 页面回滚效果(滚动条到底部刷新页面)
- 原生js封装Ajax
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
- python中的参数传递
- 人物美白
- 关于近段时间论坛型APP 的一段舍弃
- Use '$' instead of '.' for inner classes (or use only lowercase letters in package names)
- 工作两周年记------今晚和三个兄弟一起痛快地喝了一次, 在《祝你一路顺风》的旋律中!
- hdu杭电 1379 DNA Sorting
- Floyd-Warshall+二进制枚举SRM 661 Div2 Medium: BridgeBuilding
- Codeforces Round #312 (Div. 2) B.Amr and The Large Array
- vbox centos安装增强工具