java1.8 根据对象元素条件去掉集合重复数据

package com.baiyou.deWeight;

public class Student {
	private Integer stuNum; // 学号
  	private String name; // 姓名
  	private Integer age; // 年龄
	public Integer getStuNum() {
		return stuNum;
	}
	public void setStuNum(Integer stuNum) {
		this.stuNum = stuNum;
	}
	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;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((stuNum == null) ? 0 : stuNum.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 (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (stuNum == null) {
			if (other.stuNum != null)
				return false;
		} else if (!stuNum.equals(other.stuNum))
			return false;
		return true;
	}

  	
}

package com.baiyou.deWeight;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class Test {
	 
	  public static List<Student> stuList() {
	  	List<Student> stuList = new ArrayList<Student>();
	  	Student stu1 = new Student();
	  	stu1.setStuNum(20180823);
	  	stu1.setName("张三");
	  	stu1.setAge(20);
	  	stuList.add(stu1);
	 
	  	Student stu2 = new Student();
	  	stu2.setStuNum(20180824);
	  	stu2.setName("李四");
	  	stu2.setAge(21);
	  	stuList.add(stu2);
	 
	  	Student stu3 = new Student();
	  	stu3.setStuNum(20180825);
	  	stu3.setName("李四");
	  	stu3.setAge(21);
	  	stuList.add(stu3);
	 
	  	Student stu4 = new Student();
	  	stu4.setStuNum(20180826);
	  	stu4.setName("张五");
	  	stu4.setAge(22);
	  	stuList.add(stu4);
	  	return stuList;
	  }
	  
	  public static void main(String[] args) {
	  	List<Student> stuList = stuList();
	 
	    //去重
	    List<Student> distinctClass = stuList.stream().collect(Collectors.collectingAndThen(
	    		Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new));
 
	 
	  	System.out.println("之前stuList集合中的数据");
	  	stuList.forEach((Student s) -> System.out.println(s.getName() + "--->" + s.getAge()));
	  	
	  	System.out.println("原有stuList集合中的数据");
	  	distinctClass.forEach((Student s) -> System.out.println(s.getName() + "--->" + s.getAge()));
	 
	  }
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值