我正在从数据库模型 Income 获取数据。这就是它的样子
@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
and hashCode
进入我的Income
模型。
我在实现这两种方法时遇到一些麻烦。所以我也会编辑我的问题。
public int hashCode() {
return month + year;
}
public boolean equals(Object o) {
boolean flag = false;
return flag;
}
我的应该如何equals
and hashCode
执行情况看?