java - 如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项
问题描述
我正在从数据库模型收入中获取数据。这是它的样子
@Table(name = "Income")
public class Income extends Model {
@Column(name = "AmountDate")
public String amountDate;
@Column(name = "Amount")
public Double amount;
@Column(name = "Day")
public int day;
@Column(name = "Month")
public int month;
@Column(name = "Year")
public int year;
}
在我的片段中,我从数据库中获取所有收入数据。在我的 BaseAdapter 中,我想创建带有月份和年份的 ArrayList。它看起来像这样
array = [{6, 2018}, {6, 2018}, {7, 2018}, {7, 2018} {8, 2018}]
从那个数组中我想删除重复的项目,所以它应该看起来像这样array = [{6, 2018}, {7, 2018} {8, 2018}]
我将在 listview 和 onListViewItem 中显示数组中的数据,单击它将显示所选月份的所有数据。
这是我的 BaseAdapter 代码
private class IncomeArrayAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Income> incomeList;
List<Income> listOfIncomes = new ArrayList<>();
public IncomeArrayAdapter(List<Income> incomesList) {
inflater = LayoutInflater.from(getActivity());
this.incomeList = incomesList;
for (int i = 0; i < this.incomeList.size(); i++) {
listOfIncomes.add(this.incomeList.get(i));
}
Set<Income> setOfIncomes = new HashSet<>(listOfIncomes);
listOfIncomes.clear();
listOfIncomes.addAll(setOfIncomes);
for (int i = 0; i < listOfIncomes.size(); i++) {
System.out.println(listOfIncomes.get(i).month + listOfIncomes.get(i).year);
}
}
}
我对 java 很陌生,所以我的问题是如何创建新的 ArrayList 并从该列表中删除重复项?
编辑:正如我被建议实施equals
并hashCode
进入我的Income
模型。我在实现这两种方法时遇到了一些麻烦。所以我也会编辑我的问题。
public int hashCode() {
return month + year;
}
public boolean equals(Object o) {
boolean flag = false;
return flag;
}
我的实现应该equals
如何hashCode
?
解决方案
大多数 IDE 都可以自动生成 hashcode 和 equals 函数。在 IntelliJ IDEA 中,您按 Alt + Insert(或右键单击 > 生成...)
然后点击“equals() 和 hashCode()”
或者在 Eclipse 中右键单击源中的某处并选择:“Source”>“Generate hashCode() and equals()...”
无论哪种方式,它都会为您的班级产生如下内容:
public class Income extends Model {
public String amountDate;
public Double amount;
public int day;
public int month;
public int year;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((amount == null) ? 0 : amount.hashCode());
result = prime * result + ((amountDate == null) ? 0 : amountDate.hashCode());
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Income other = (Income) obj;
if (amount == null) {
if (other.amount != null)
return false;
} else if (!amount.equals(other.amount))
return false;
if (amountDate == null) {
if (other.amountDate != null)
return false;
} else if (!amountDate.equals(other.amountDate))
return false;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
推荐阅读
- javascript - 使用树构造树的问题
- ios - setNavigationBarHidden 不起作用
- c++-cli - 我在 C++ CLI 中创建了一个项目,但我可以使用 C# 反编译器对其进行反编译
- java - Bitmap.getPixel(x, y) 的 RGB 方法在不同设备上返回的值略有不同
- alibaba-cloud - 在 Android 应用程序中引导阿里云消息服务?
- html - 使用 UIKit/HTML5 垂直适合带有滚动的中间元素
- java - “无法解析构造函数标签(java.lang.String)”
- c# - Visual Studio 安装程序项目使用旧的安装程序
- javascript - Django Rest 框架:HTTP 401 未经授权的错误
- python - 将混合类型的 DataFrame 拆分为两列?