Skip to content
Advertisement

How to filter an array of objects by month and year – Java

I’m new at Java and I have a question. I have this array of objects:

JavaScript

That contains this:

enter image description here

I would like to know how can I filter this array by month and year.

A first example would be filter the array by month 10 and year 2021.

Any suggestion ?

Advertisement

Answer

You can use the Stream API to do it as follows:

JavaScript

Alternatively,

JavaScript

I recommend you do it in the second way to avoid Expense#getDate getting called twice in each step.

Advertisement