I'm getting data from database model Income. This is how it looks
@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;
}
In my fragment I'm getting all Income data from database. In my BaseAdapter I would like to create ArrayList with month and year in it. It would look something like this
array = [{6, 2018}, {6, 2018}, {7, 2018}, {7, 2018} {8, 2018}]
and from that array I would like to remove duplicate items so it should look like this array = [{6, 2018}, {7, 2018} {8, 2018}]
And that data from array I would be showing in listview and onListViewItem click it will show all data for selected month.
Here is my BaseAdapter code
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);
}
}
}
I'm pretty new to java so my question is how to create new ArrayList and remove duplicates from that list?
EDIT:
As I was suggested to implement equals
and hashCode
into my Income
model.
I have some trouble with an implementation of those two methods. So I will also edit my question.
public int hashCode() {
return month + year;
}
public boolean equals(Object o) {
boolean flag = false;
return flag;
}
How should my equals
and hashCode
implementation look?