如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项 [英] How to implement hashCode and equals method to remove duplicates from ArrayList

查看:23
本文介绍了如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库模型收入中获取数据.这是它的样子

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

在我的片段中,我从数据库中获取所有收入数据.在我的 BaseAdapter 中,我想创建包含月份和年份的 ArrayList.它看起来像这样

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

并且我想从该数组中删除重复项,因此它应该如下所示 array = [{6, 2018}, {7, 2018} {8, 2018}]我将在列表视图和 onListViewItem 中显示来自数组的数据,单击它将显示所选月份的所有数据.

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.

这是我的 BaseAdapter 代码

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

我对 Java 很陌生,所以我的问题是如何创建新的 ArrayList 并从该列表中删除重复项?

I'm pretty new to java so my question is how to create new ArrayList and remove duplicates from that list?

因为有人建议我在我的 Income 模型中实现 equalshashCode.我在实现这两种方法时遇到了一些麻烦.所以我也会编辑我的问题.

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

我的 equalshashCode 实现应该是什么样的?

How should my equals and hashCode implementation look?

推荐答案

大多数 IDE 都有自动生成 hashcode 和 equals 函数的方法.在 IntelliJ IDEA 中,按 Alt + Insert(或右键单击 > Generate...)

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

然后点击equals() and hashCode()"

Then click "equals() and hashCode()"

或者在 Eclipse 中右键单击源代码的某个位置并选择:"Source" > "生成 hashCode() 和 equals()..."

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

这篇关于如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆