RubyDigger19
RubyDigger19

Reputation: 835

How to create new array from existing without duplicate elements

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.

Upvotes: 0

Views: 332

Answers (3)

NiVeR
NiVeR

Reputation: 9786

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.

Upvotes: 1

Bharat Satija
Bharat Satija

Reputation: 362

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

Upvotes: 1

Dan
Dan

Reputation: 1033

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.

Upvotes: 0

Related Questions