java - 如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项

标签 java android arraylist baseadapter hashcode

我正在从数据库模型 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;
}

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

数组 = [{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 并从该列表中删除重复项?

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

  public int hashCode() {
        return month + year;
    }

    public boolean equals(Object o) {
        boolean flag = false;
        return flag;
    }

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

最佳答案

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

IntelliJ Generate Menu

然后单击“equals() 和 hashCode()”

或者在 Eclipse 中右键单击源代码上的某处并选择: “来源”>“生成 hashCode() 和 equals()...”

Eclipse Right Click Menu

无论哪种方式,它都会为您的类(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;
    }
}

关于java - 如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51487805/

相关文章:

java - 如何创建一个在给定范围内随机打乱数字的 int 数组

java - 在过滤器之后使用 Java Spark 在发生 404 时运行自定义操作

java - 创建一个不同的列表<String>

java - 为什么 ArrayList<>.add() 不起作用?

c# - 二元搜索和indexof 哪个更快?

java - 如何将字符串解析为数组

Java/JVM(热点): Is there a way to save JIT performance gains at compile time?

java - 使用 try-catch 验证输入

android - 在改造中传递字符串数组在响应中的每个字符串中给出\"

android - 将可绘制对象添加到 CirclePageIndicator