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.

5
  • What is the problem with your current approach? Commented Jul 24, 2018 at 17:38
  • I have 5 objects in incomeList and listOfIncomes returns the same list Commented Jul 24, 2018 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). Commented Jul 24, 2018 at 17:46
  • Possible duplicate of Remove duplicates from a list of objects based on property in Java 8 Commented Jul 24, 2018 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 Commented Jul 24, 2018 at 17:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Obviously, there is something wrong with my equals method because I'm getting back the list with all items in it
@RubyDigger19 You were not giving an Income object to the contains method. Did you change that?
How should I do that?
Look at the code I posted. There is incomesList.get(i) as argument. You are doing some kind of append.
I have this incomesList.get(i) but I don't get it
|
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));
     }
 }

3 Comments

@RubyDigger19 you are passing incomesList.get(i).month + incomesList.get(i).year in contains method you have to pass Income object -> incomesList.get(i) .
I've create code like this but still getting the same array as original one
@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.
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.

4 Comments

I've edited the question with my hashCode and equals. Is it oke if you could check?
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
Did like you said but still getting the same array with duplicates
So then something is up with your equals and/or hashCode. Step through the values in the debugger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.