• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Stream Issue of calculating count, sum of one value and multiple group by conditions

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



Here is the list



What I want to do is to get this result.



Here is group dto shown below



Here is my dto shown below.




Here is the code snippet but I cannot handle with it.



Here is the same result



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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code looks a bit like trying to produce the implementation before you worked out exactly what you want to do. It also looks as if your classes didn't have enough methods. It might be unusual to get the month without the rest of the date, but in this case I think Employee should have a getStartMonth() method if you are using that month number.
Streams work best with straightforward operations, and a two‑stage procedure might be easier.
Please explain more; we need details and information.
Is Statement a two‑element enum? Does values() return a List<Employee>?
Why are you using BigDecimals for something with values 1, 2? Why is it declared as Object? Are you going to have to cast that field and have the cast fail?
Why can't you simply use month and groupingBy? That will of course give you a Map<Integer, List<Employee>>.If you group by the id String instead, it should be easy enough to traverse the resulting Map and convert the List<Employee> to another object encapsulating the Lists, with a method to return the Lists' size()s.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your approach didn't help me fix the issue.

I'm getting incorrect count totalPerson because I'm not taking into account person id.
I'm treating each DTO in your code as representing a distinct Person which is not true

How can I do that?

How can I count Person object by using an intermediate object containing set of id as it is coming in my mind.


It didn't help me fix the issue as shown below code


 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the way to solve the issue.Firstly I can create an intermediate object maintaining a set of person id to accumulate the Person instances. And then turn these objects into DTO by using the size of their Sets of ids as the person count. However, I have no idea how to implement it for my issue which I mentioned before.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didn't help, but is there a simpler way you can do that? What exactly do you want your result to look like?
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already shared what I want to get the result.



I calculate the total count of person wrongly as you can see. It is my issue which I cannot tackle
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post a copy of a text file from which you would populate your List.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already shared it the post.
,
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though now, it no longer has "..." indicating it was incomplete.

Is a DTO the same as a Dto or a GroupDto?  Is a Statement.STATUS1 the same as a Value.STATUS1?  These class names seem to change randomly.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DTO is the same as dto not GroupDto as you can see.
Statement.STATUS1 is the same as Value.STATUS1

Can you focus on my issue?
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I can see, DTO is not the same as Dto.  Nor is Statement the same as Value.  Surely your compiler would agree.  I think you'll find if you want help, it goes easier if your code is not unnecessarily confusing to the reader.

I think there's probably a way to do what you want using Collectors.teeing() and other methods - I'm working on that...
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no problem in defining the class.

Problem is based on totalPersons.

As you can see, it calculate the wrong result.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are obvious problems in the code you've actually shown.  You've probably fixed those on your end, but not shown them, so it creates more work for people looking at your code to figure out WTF is going on.  I also see that "value" has become "totalSalary" and changed from an Object to a BigDecimal.  That's good, but again, if you'd shown the code correctly in the first place, it would be easier to work with.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can get the month value and calculate the sum value even if I cannot get the right result of total count. This is my issue.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can also used this option but it didn't help me get the result.








 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't yet looked carefully at your last two posts, sorry.  But here's what I came up with:


 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternately:


And then:

This has a couple advantages over the previous version: it doesn't store up as much data in memory, and it doesn't do multiple passes through the data to get what it needs.
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I haven't yet looked carefully at your last two posts, sorry.  But here's what I came up with:




I have a problem in here -> Map.Entry::getKey. Iy throws an message named "Non-static method cannot be referenced from a static context"

Here is the my code.



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you write Map.Entry::getKey in the first place?
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my example, I wrote Map.Entry::getKey because there was previously a Map<Integer,List<Person>>, and we need to replace the List<Person> with a Dto.  Se we do .entrySet().stream() to get a stream of Map.Entry objects, and then can re-map them to different values.  It's a little inefficient, but it was hard otherwise to combine the three things we need: the month, the count, and the total salary.

Now, why is it not working in Kevin's code?  I don't know.  One problem with streams is if something's just a little off, you can get very confusing error messages.  It can be helpful to introduce intermediate variables to double check the type along the way.

I am pretty sure that the final result of this code should be a Map<Integer,Dto>, so saying that the result employeeList is a List<Employee> is probably going to confuse it.

Let's try checking what's going on before we get to the final result:

Kevin, what errors do you get here?
 
Kevin Rapter
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I get the same result.



How can I fix it?
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I did that one too quickly.  Try replacing this:

with this:
reply
    Bookmark Topic Watch Topic
  • New Topic