0

I have an array of objects in java. Something like this

array = [{month: 6, year: 2018, amount: 242}, {month: 6, year: 2018, amount: 345}, {month: 7, year: 2018, amount: 4567}, {month: 7, year: 2018, amount: 1234}, {month: 8, year: 2018, amount: 211}]

So I want to create a new array without duplicate month and year keys so this new array should look like this

newArray = [{month: 6, year: 2018}, {month: 7, year: 2018}, {month: 8, year: 2018}]

Here is function that I've implemented

private class IncomeArrayAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<Income> incomeList;
        List<Income> listOfIncomes = new ArrayList<>();

        public IncomeArrayAdapter(List<Income> incomesList) {
            boolean found = false;
            inflater = LayoutInflater.from(getActivity());
            this.incomeList = incomesList;
            for (int i = 0; i < incomeList.size(); i++) {
                if (!listOfIncomes.contains(incomesList.get(i).month + incomesList.get(i).year)) {
                    listOfIncomes.add(incomesList.get(i));
                }
            }
}

So I've tried with hashCode() and equals() functions but I'm new to java and I failed. So I would like if some could help me and explain to me how to create this new array?

EDIT:

   @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Income income = (Income) o;
        return month == income.month &&
                year == income.year;
    }

    @Override
    public int hashCode() {

        return Objects.hash(super.hashCode(), month, year);
    }

here is my hashCode() and equals() implementation. It should return only one list object if month and year are equal.

RubyDigger19
  • 835
  • 13
  • 38
  • What is the problem with your current approach? – NiVeR Jul 24 '18 at 17:38
  • I have 5 objects in incomeList and listOfIncomes returns the same list – RubyDigger19 Jul 24 '18 at 17:43
  • 1
    hint: For `contains` to work the way you want it to, you should be passing an `Income` to it. It looks like you're passing either a `String` or an `int` (depending on what the types of `month` and `year` are). – VeeArr Jul 24 '18 at 17:46
  • Possible duplicate of [Remove duplicates from a list of objects based on property in Java 8](https://stackoverflow.com/questions/29670116/remove-duplicates-from-a-list-of-objects-based-on-property-in-java-8) – Krzysztof Zieliński Jul 24 '18 at 17:47
  • What i suggest you is too redefine your class `Income` extends new abstract class `AbstractIncome` where you put filed `month` and `years` and you put `amount` in the `Income` class – Crammeur Jul 24 '18 at 17:48

3 Answers3

1

Assuming your equals implementation is correct, you should modify the if-statement in the following way to make use of it:

...
if (!listOfIncomes.contains(incomesList.get(i))) 
{
    listOfIncomes.add(incomesList.get(i));
} 
...

also another suspicious thing that I see is in the equals implementation, in particular the line:

if (!super.equals(o)) return false;

try removing it and try again.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
1

You have to update if statement and pass income object in contains method. contains method internally calls equals methods.

 for (int i = 0; i < incomeList.size(); i++) { 
     if (!listOfIncomes.contains(incomesList.get(i)) {
                listOfIncomes.add(incomesList.get(i));
     }
 }
Bharat Satija
  • 362
  • 1
  • 13
  • @RubyDigger19 you are passing `incomesList.get(i).month + incomesList.get(i).year` in `contains` method you have to pass `Income object -> incomesList.get(i)` . – Bharat Satija Jul 24 '18 at 18:09
  • I've create code like this but still getting the same array as original one – RubyDigger19 Jul 24 '18 at 18:14
  • 1
    @RubyDigger19 I just ran the code and removed this if statement `if (!super.equals(o)) return false;` and it worked for me.Can you remove this line `if (!super.equals(o)) return false;` from the code and try. – Bharat Satija Jul 24 '18 at 18:29
0

Set<Income> uniqueSets = Sets.newHashSet(incomesList); should do the trick.

Just make sure the equals and hashCode methods are setup the way you're expecting the logic to work.

Dan
  • 979
  • 1
  • 8
  • 29
  • I've edited the question with my `hashCode` and `equals`. Is it oke if you could check? – RubyDigger19 Jul 24 '18 at 17:48
  • Your `equals` and `hashcode` only seem to care about the month and year. I'd just make sure those values are the same. I'd convert the `List` into a `Set`, which will make all the values unique. If necessary, then convert it back or just return the `Set` – Dan Jul 24 '18 at 17:52
  • Did like you said but still getting the same array with duplicates – RubyDigger19 Jul 24 '18 at 18:01
  • So then something is up with your `equals` and/or `hashCode`. Step through the values in the debugger – Dan Jul 24 '18 at 18:04