How to implement hashCode and equals method to remove duplicates from ArrayList

故事扮演 提交于 2020-12-13 02:57:07

问题


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?


回答1:


Most IDEs will have a way to auto generate hashcode and equals functions. In IntelliJ IDEA you press Alt + Insert (or right click > Generate...)

Then click "equals() and hashCode()"

Or in Eclipse you right click somewhere on the source and select: "Source" > "Generate hashCode() and equals()..."

Either way, it will produce something like the following for your class:

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;
    }
}


来源:https://stackoverflow.com/questions/51487805/how-to-implement-hashcode-and-equals-method-to-remove-duplicates-from-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!