2

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?

RubyDigger19
  • 835
  • 13
  • 38
  • 2
    Possible duplicate of [How do I remove repeated elements from ArrayList?](https://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – Philipp Jul 23 '18 at 22:15
  • See my duplicate comment and be sure you have an `equals` and `hashCode` implementation in your `Income` class, see https://www.geeksforgeeks.org/equals-hashcode-methods-java/ – Philipp Jul 23 '18 at 22:16
  • Thanks for your answer. I'll try to implement `equals` and `hashCode`. But don't know will I succed – RubyDigger19 Jul 23 '18 at 22:22

1 Answers1

3

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

IntelliJ Generate Menu

Then click "equals() and hashCode()"

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

Eclipse Right Click Menu

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;
    }
}
Lunchbox
  • 1,603
  • 11
  • 14
  • Thanks. and then in my class, I just call hashCode? – RubyDigger19 Jul 23 '18 at 23:08
  • Nope, now the code you have works because 'listOfIncomes.addAll(setOfIncomes);' will take care of only inserting the items once. That method relies on your Income class having properly implemented hashCode and equals methods. – Lunchbox Jul 24 '18 at 18:47