Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Java Stream Not Calculating the total count after filtering and grouping by

datdatyul

Active Coder
I have a problem about writing a java stream by filtering multiple conditions , groupby multiple condition (if possible) and calculating the sum value

I store the values as `Map<String(pId),List<Person>>`


Here is my Person class shown below
Code:
    public class Person{
    
        private String id;
        private Statement event; // STATUS1,STATUS2
        private LocalDate eventDate;
        private Object value;
    }

Here is the list
Code:
    ID,Info,Date,Value (All values are stored in Person object defined in the List)
    
    per1, STATUS1,  10-01-2022, 1
    per2, STATUS2, 10-01-2022, 2
    per3, STATUS3, 10-01-2022, 3
    per1, STATUS1, 10-01-2022, 1  
    per1 STATUS1, 10-02-2022, 1
    per2, STATUS1, 10-03-2022, 1
    per3, STATUS2, 10-03-2022, 2
    ...
    ...

What I want to do is to get this result.
Code:
    Month |  Total Sum | Person Count
    1        7          3
    2        1          1
    3        3          2
Here is group dto shown below
Code:
    public class GroupDto {
            private int month;
            private String id;
    }

Here is my dto shown below.
Code:
    public class DTO {
        private int month;
        private BigDecimal totalSum; 
        private int totalPersons;  
    }

Here is the code snippet but I cannot handle with it.
Code:
    List<Dto> group = employees.values().stream()
                    .flatMap(List::stream)
                    .filter(emp -> emp.getEvent() == Value.STATUS1 || emp.getEvent() == Value.STATUS2 
                            || emp.getEvent() == Value.STATUS3 )
                    .map(emp -> new Dto(emp.getEventDate().getMonthValue(), new BigDecimal(emp.getValue().toString()), 1))
                    .collect(Collectors.toList());
    
            List<Dto> result = new ArrayList<>(group.stream()
                    .collect(Collectors.toMap(
                            dto -> Arrays.asList(dto.getMonth()), Function.identity(), Dto::aggregate))
                    .values()).stream().sorted(Comparator.comparing(Dto::getMonth))
                    .collect(Collectors.toList());
    
    public static Dto aggregate(Dto initial, Dto next) {
            initial.setTotalSalary(initial.getTotalSalary().add(next.getTotalSalary()));
            initial.setTotalEmployees(initial.getTotalEmployees() + next.getTotalEmployees());
            return initial;
        }

    result.forEach(System.out::println);

Here is the same result
Code:
    Dto{month=1, totalSalary=7, totalPersons=4}
    Dto{month=2, totalSalary=1, totalPersons=1}
    Dto{month=3, totalSalary=3, totalPersons=2}
Normally, total persons in month 1 is 3 but it counts 4 with respect to status.

My Issue : I cannot count the person, It counts by status. How can I do that?
 

New Threads

Buy us a coffee!

Back
Top Bottom