获取两个List中重复或不同的元素---removeAll()和addAll()的使用

昨天项目中遇到了一个将查询结果增量发出的,即每次比上次增的获取。

这个问题同事去网上找了好多高人写的代码,我看了好久,感觉被绕了,最后想着还是用最简单的方式来做,可能好一些。

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]]
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值