-1

I'm trying to filter or limit between two dates (Locale Date), and I'm not sure how to do it in the code below.

My Code

        Map<String, Double> Total_IExpense = datatransaction.stream()
                .collect(groupingBy(TransactionData::getName,
                        Collectors.summingDouble(TransactionData::getAmount)));
Date        Name    Amount

04-Jan-21   Jomon   3,900.00
01-Jan-21   John    650.00
29-Dec-20   Priya   1,435.00
28-Dec-20   Jore    303.00
28-Dec-20   Jomon   453.74
28-Dec-20   Jore    81.67
26-Dec-20   John    25.12
21-Nov-20   Priya   118.38
21-Nov-20   Jomon   21.31
20-Nov-20   John    438.24
20-Nov-20   John    78.88
19-Nov-20   Jai 1,612.81
19-Nov-20   Priya   68.22

I required to filter - 21-Nov-20 to 28-Dec-20 and expected output

Date        Name    Amount
21-Nov-20   Jomon   21.31
21-Nov-20   Priya   118.38
26-Dec-20   John    25.12
28-Dec-20   Jomon   453.74
28-Dec-20   Jore    384.67
Ajit V
  • 1
  • 3
  • where does the `Date` come from your model ? I only see a `String` and `Doubles`. Btw if you find a way to manipulate a Date, you can filter your stream using prebuilt comparaison function from LocalDate objetcs – stephane brun Nov 04 '21 at 08:25
  • 1
    a `LocalDate` (assuming that is meant by "Locale Date") has methods `isAfter()` and `isBefore()` that can be user in a `filter(...)` in your `Stream`. (if still using the ancient `java.util.Date` class, it has `after()` and `before()`) – user16320675 Nov 04 '21 at 09:27
  • I am unsure what is stopping you. Does this answer your question? [Java: how do I check if a Date is within a certain range?](https://stackoverflow.com/questions/494180/java-how-do-i-check-if-a-date-is-within-a-certain-range) – Ole V.V. Nov 04 '21 at 10:46
  • Do you want grouping by *date **and** name*? It seems to me the two rows `28-Dec-20 Jore 303.00` and `28-Dec-20 Jore 81.67` have been summed into `28-Dec-20 Jore 384.67` in your required/expected output whereas the two rows with name `Jomon` have not been added together. – Ole V.V. Nov 05 '21 at 04:44
  • Does this answer your question? [Group by multiple field names in java 8](https://stackoverflow.com/questions/28342814/group-by-multiple-field-names-in-java-8) – Ole V.V. Nov 05 '21 at 04:46

1 Answers1

1

I think you should filter a stream based on a predicate (condition) provided as an argument with the filter() method.

In case you need to filter a string stream and only return strings starting with ’n’

strFlux.filter(s->s.startsWith("n")).log();

if length of a string is greater than 4, then return it’s length as an element of the stream.

strFlux.filter(s -> s.length() > 4).map(s->s.length()).log(); //Now it will be a Flux<Integer>