Java List对象分组
实体类
必须重写equals和hashCode方法
package com.zcsoft.rc.backend.biz.vo.securityLibary; import java.util.Date; import java.util.Objects; /** * @ClassName:SecurityLibraryImportDistinctVO * @Description:安全风险库导入数据分组,根据导入数据分主表数据 * @Author:chenyb * @Date:2020/8/13 11:30 上午 * @Versiion:1.0 */ public class SecurityLibraryImportDistinctVO { /** * 内码 */ private String id; /** * 风险类别 */ private String riskType; /** * 风险名称 */ private String riskName; /** * 风险点 */ private String riskPoint; /** * 风险点里程 */ private String riskPointMileage; /** * 安全风险点描述 */ private String riskDesc; /** * 可能造成的后果 */ private String causeResult; /** * 风险等级 */ private String riskLevel; /** * 风险期限 */ private Date riskPeriod; /** * 创建时间 */ private Date createTime; /** * 修改时间 */ private Date modifyTime; public String getRiskType() { return riskType; } public void setRiskType(String riskType) { this.riskType = riskType; } public String getRiskName() { return riskName; } public void setRiskName(String riskName) { this.riskName = riskName; } public String getRiskPoint() { return riskPoint; } public void setRiskPoint(String riskPoint) { this.riskPoint = riskPoint; } public String getRiskPointMileage() { return riskPointMileage; } public void setRiskPointMileage(String riskPointMileage) { this.riskPointMileage = riskPointMileage; } public String getRiskDesc() { return riskDesc; } public void setRiskDesc(String riskDesc) { this.riskDesc = riskDesc; } public String getCauseResult() { return causeResult; } public void setCauseResult(String causeResult) { this.causeResult = causeResult; } public String getRiskLevel() { return riskLevel; } public void setRiskLevel(String riskLevel) { this.riskLevel = riskLevel; } public Date getRiskPeriod() { return riskPeriod; } public void setRiskPeriod(Date riskPeriod) { this.riskPeriod = riskPeriod; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getModifyTime() { return modifyTime; } public void setModifyTime(Date modifyTime) { this.modifyTime = modifyTime; } @Override public String toString() { return "SecurityLibraryImportDistinctVO{" + "id='" + id + '\'' + ", riskType='" + riskType + '\'' + ", riskName='" + riskName + '\'' + ", riskPoint='" + riskPoint + '\'' + ", riskPointMileage='" + riskPointMileage + '\'' + ", riskDesc='" + riskDesc + '\'' + ", causeResult='" + causeResult + '\'' + ", riskLevel='" + riskLevel + '\'' + ", riskPeriod=" + riskPeriod + ", createTime=" + createTime + ", modifyTime=" + modifyTime + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof SecurityLibraryImportDistinctVO)) return false; SecurityLibraryImportDistinctVO that = (SecurityLibraryImportDistinctVO) o; return Objects.equals(riskType, that.riskType) && Objects.equals(riskName, that.riskName) && Objects.equals(riskPoint, that.riskPoint) && Objects.equals(riskPointMileage, that.riskPointMileage) && Objects.equals(riskDesc, that.riskDesc) && Objects.equals(causeResult, that.causeResult) && Objects.equals(riskLevel, that.riskLevel) && Objects.equals(riskPeriod, that.riskPeriod); } @Override public int hashCode() { return Objects.hash(riskType, riskName, riskPoint, riskPointMileage, riskDesc, causeResult, riskLevel, riskPeriod); } }
工具类
package com.zcsoft.rc.backend.biz.util; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * @ClassName:ListUtils * @Description:List工具类 * @Author:chenyb * @Date:2020/8/13 11:26 上午 * @Versiion:1.0 */ public class ListUtils <T>{ /** * 通过set去重, 不打乱原有list的顺序 * list中相同的对象会被去重复 * * @param List<T> list * @return List<T> * */ public List<T> distinctBySetOrder(List<T> list){ Set<T> set = new HashSet<T>(); List<T> newList = new ArrayList<T>(); for (T t :list) { if(set.add(t)){ newList.add(t); } } return newList; } /** * 通过set去重, 顺序可能会乱 * list中相同的对象会被去重复 * * @param List<T> list * @return List<T> * */ public List<T> distinctBySet(List<T> list){ return new ArrayList<T>(new HashSet<T>(list)); } }
使用
List<SecurityLibraryImportDistinctVO> list=new ArrayList<>(); //使用工具类 ListUtils<SecurityLibraryImportDistinctVO> listUtils=new ListUtils<SecurityLibraryImportDistinctVO>(); //分组后的List对象 List<SecurityLibraryImportDistinctVO> distinctList = listUtils.distinctBySet(list);