java list 去重 排序_JAVA list对象排序加去重问题

本文介绍了一种在Java中实现对象排序与去重的方法。通过实现Comparable接口并重写compareTo方法来根据个人正确率进行排序,同时通过重写equals方法实现基于ID的去重功能。文章提供了一个具体示例,展示了如何利用Collections.sort()和TreeSet进行操作。
摘要由CSDN通过智能技术生成

对象类实现继承Comparable接口重写compareTo方法实现排序功能,重写equals方法实现去重功能(根据ID去重)

public class TestAbilityAnalyze implements Comparable {

private String abilityID;

/**

* 能力名称

*/

private String abilityName;

/**

* 个人正确率

*/

private double accuracy;

/**

* 班级正确率

*/

private double averageByClassID;

/**

* 年级正确率

*/

private double averageByGradeID;

public String getAbilityID() {

return abilityID;

}

public void setAbilityID(String abilityID) {

this.abilityID = abilityID;

}

public String getAbilityName() {

return abilityName;

}

public void setAbilityName(String abilityName) {

this.abilityName = abilityName;

}

public double getAccuracy() {

return accuracy;

}

public void setAccuracy(double accuracy) {

this.accuracy = accuracy;

}

public double getAverageByClassID() {

return averageByClassID;

}

public void setAverageByClassID(double averageByClassID) {

this.averageByClassID = averageByClassID;

}

public double getAverageByGradeID() {

return averageByGradeID;

}

public void setAverageByGradeID(double averageByGradeID) {

this.averageByGradeID = averageByGradeID;

}

@Override

public int compareTo(TestAbilityAnalyze o) {

// TODO Auto-generated method stub

return (int) (this.getAccuracy() - o.getAccuracy());

}

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (!(obj instanceof Test))

return false;

TestAbilityAnalyze other = (TestAbilityAnalyze) obj;

if (abilityID == null) {

if (other.abilityID == null)

return true;

else

return false;

} else {

if (abilityID.equals(other.abilityID))

return true;

else

return false;

}

}

}

根据正确率排序,然后使用set去重

Collections.sort(analyzes);

Set result = new TreeSet<>((o1, o2) -> o1.getAbilityID().compareTo(o2.getAbilityID()));

result.addAll(analyzes);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值