本文转载:
原文地址: List集合去重的三种方法
博客观看总结:
我采用 “ 2、java 8中去重操作 ” 的方式来给 List集合中的对象去重。通过重写对象中的 hashCode()
和equals()
方法,实现了去重。
注意,这两个函数都需要重写。可以使用 Intellij IDEA 自动生成代码。
在这里,我贴一个 Student.java 文件中 hashCode()、equals() 中的重写内容,希望能够帮到大家。
package com.moon.learning.common.pojo;
import com.moon.learning.string.String03;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {
private static final long serialVersionUID = 2885969674350877756L;
/**
* 学生ID
*/
@ApiModelProperty(value = "学生ID", name = "id", position = 10, required = true)
private Long id;
/**
* 姓名
*/
@ApiModelProperty(value = "姓名", name = "name", position = 20, required = true)
private String name;
/**
* 年龄
*/
@ApiModelProperty(value = "年龄", name = "age", position = 30, required = true)
private Integer age;
/**
* 性别: 0:女性, 1: 男性
*/
@ApiModelProperty(value = "性别", name = "sex", position = 40, required = true)
private Integer sex;
/**
* 地址
*/
@ApiModelProperty(value = "地址", name = "address", position = 50, required = true)
private String address;
public void change(Student student) {
student.setName("柳乐儿");
student.setAddress("中国-上海市-浦东新区-吃饭镇.");
}
/**
* 初始化 student.
*
* @param student
*/
public void initStudent(Student student) {
this.name = student.getName() + "_小红花";
this.age = student.getAge().hashCode() + 20;
}
// ------------------- 重写函数 -------------------
/**
* 重写 equals() 函数
*
* @return
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(id, student.id) &&
Objects.equals(name, student.name) &&
Objects.equals(age, student.age) &&
Objects.equals(sex, student.sex) &&
Objects.equals(address, student.address);
}
/**
* 重写 hashCode() 函数
*
* @return
*/
@Override
public int hashCode() {
return Objects.hash(id, name, age, sex, address);
}
// ------------------- get/set函数、构造函数 -------------------
/**
* 构造函数1
*
* @param id
* @param name
* @param age
* @param address
*/
public Student(Long id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
/**
* 全参构造函数
*
* @param id
* @param name
* @param age
* @param sex
* @param address
*/
public Student(Long id, String name, Integer age, Integer sex, String address) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
}
/**
* 无参构造函数
*/
public Student() {
}
public Long getId() {
return id;
}
public void setId(Long 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;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", address='" + address + '\'' +
'}';
}
}
------------------ 2023/01/04 14:32 ------------------
在与同行交流的过错中,使用 Lombox 中的 @Data注解添加到 pojo 实体类上,直接实现了重写 hashCode() 和 equals() 方法 。
感谢同行小伙伴的分享。
感恩相遇