首先重写equals方法和hascode方法 里面可以自定义判断根据你需要的属性这个对象是否相同
package com.web.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
@Data
public class TbHuidou implements Serializable {
private static final long serialVersionUID = 1L;
private Integer hid;
private String acyivity_name;
private String dzh_no;
private String cf_no;
private String lc_no;
private String product_code;
private String product_type;
private String function_id;
private Integer sent;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
//@com.fasterxml.jackson.annotation.JsonIgnore
private Date create_time;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
// @com.fasterxml.jackson.annotation.JsonIgnore
private Date update_time;
private Integer is_delete;
public TbHuidou() {
}
/**
* 重写eques方法
* @param o
* @return
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TbHuidou tbHuidou = (TbHuidou) o;
return dzh_no.equals(tbHuidou.dzh_no) && lc_no.equals(tbHuidou.lc_no) && product_code.equals(tbHuidou.product_code) && function_id.equals(tbHuidou.function_id);
}
/**
* 重写hascode方法
* @return
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 17;
result = prime * result + (dzh_no == null ? 0 : dzh_no.hashCode());
result = prime * result + (lc_no == null ? 0 : lc_no.hashCode());
result = prime * result + (product_code == null ? 0 : product_code.hashCode());
result = prime * result + (function_id == null ? 0 : function_id.hashCode());
return result;
}
}
set集合去重复
List<TbHuidou> tbHuidous = tbHuidouMapper.selectHuidouByDate();
Set<TbHuidou> ts = new HashSet<>();
ts.addAll(tbHuidous);
System.err.println(ts);